… | |
… | |
15 | }); |
15 | }); |
16 | |
16 | |
17 | my $w = AnyEvent->condvar; # stores wether a condition was flagged |
17 | my $w = AnyEvent->condvar; # stores wether a condition was flagged |
18 | $w->wait; # enters "main loop" till $condvar gets ->broadcast |
18 | $w->wait; # enters "main loop" till $condvar gets ->broadcast |
19 | $w->broadcast; # wake up current and all future wait's |
19 | $w->broadcast; # wake up current and all future wait's |
|
|
20 | |
|
|
21 | WHY YOU SHOULD USE THIS MODULE (OR NOT) |
|
|
22 | Glib, POE, IO::Async, Event... CPAN offers event models by the dozen |
|
|
23 | nowadays. So what is different about AnyEvent? |
|
|
24 | |
|
|
25 | Executive Summary: AnyEvent is *compatible*, AnyEvent is *free of |
|
|
26 | policy* and AnyEvent is *small and efficient*. |
|
|
27 | |
|
|
28 | First and foremost, *AnyEvent is not an event model* itself, it only |
|
|
29 | interfaces to whatever event model the main program happens to use in a |
|
|
30 | pragmatic way. For event models and certain classes of immortals alike, |
|
|
31 | the statement "there can only be one" is a bitter reality, and AnyEvent |
|
|
32 | helps hiding the differences. |
|
|
33 | |
|
|
34 | The goal of AnyEvent is to offer module authors the ability to do event |
|
|
35 | programming (waiting for I/O or timer events) without subscribing to a |
|
|
36 | religion, a way of living, and most importantly: without forcing your |
|
|
37 | module users into the same thing by forcing them to use the same event |
|
|
38 | model you use. |
|
|
39 | |
|
|
40 | For modules like POE or IO::Async (which is actually doing all I/O |
|
|
41 | *synchronously*...), using them in your module is like joining a cult: |
|
|
42 | After you joined, you are dependent on them and you cannot use anything |
|
|
43 | else, as it is simply incompatible to everything that isn't itself. |
|
|
44 | |
|
|
45 | AnyEvent + POE works fine. AnyEvent + Glib works fine. AnyEvent + Tk |
|
|
46 | works fine etc. etc. but none of these work together with the rest: POE |
|
|
47 | + IO::Async? no go. Tk + Event? no go. If your module uses one of those, |
|
|
48 | every user of your module has to use it, too. If your module uses |
|
|
49 | AnyEvent, it works transparently with all event models it supports |
|
|
50 | (including stuff like POE and IO::Async). |
|
|
51 | |
|
|
52 | In addition of being free of having to use *the one and only true event |
|
|
53 | model*, AnyEvent also is free of bloat and policy: with POE or similar |
|
|
54 | modules, you get an enourmous amount of code and strict rules you have |
|
|
55 | to follow. AnyEvent, on the other hand, is lean and to the point by only |
|
|
56 | offering the functionality that is useful, in as thin as a wrapper as |
|
|
57 | technically possible. |
|
|
58 | |
|
|
59 | Of course, if you want lots of policy (this can arguably be somewhat |
|
|
60 | useful) and you want to force your users to use the one and only event |
|
|
61 | model, you should *not* use this module. |
20 | |
62 | |
21 | DESCRIPTION |
63 | DESCRIPTION |
22 | AnyEvent provides an identical interface to multiple event loops. This |
64 | AnyEvent provides an identical interface to multiple event loops. This |
23 | allows module authors to utilise an event loop without forcing module |
65 | allows module authors to utilise an event loop without forcing module |
24 | users to use the same event loop (as only a single event loop can |
66 | users to use the same event loop (as only a single event loop can |
… | |
… | |
65 | You can create I/O watcher by calling the "AnyEvent->io" method with the |
107 | You can create I/O watcher by calling the "AnyEvent->io" method with the |
66 | following mandatory arguments: |
108 | following mandatory arguments: |
67 | |
109 | |
68 | "fh" the Perl *filehandle* (not filedescriptor) to watch for events. |
110 | "fh" the Perl *filehandle* (not filedescriptor) to watch for events. |
69 | "poll" must be a string that is either "r" or "w", that creates a |
111 | "poll" must be a string that is either "r" or "w", that creates a |
70 | watcher waiting for "r"eadable or "w"ritable events. "cb" teh callback |
112 | watcher waiting for "r"eadable or "w"ritable events. "cb" the callback |
71 | to invoke everytime the filehandle becomes ready. |
113 | to invoke everytime the filehandle becomes ready. |
72 | |
114 | |
73 | Only one io watcher per "fh" and "poll" combination is allowed (i.e. on |
115 | Only one io watcher per "fh" and "poll" combination is allowed (i.e. on |
74 | a socket you can have one r + one w, not any more (limitation comes from |
116 | a socket you can have one r + one w, not any more (limitation comes from |
75 | Tk - if you are sure you are not using Tk this limitation is gone). |
117 | Tk - if you are sure you are not using Tk this limitation is gone). |
… | |
… | |
84 | chomp (my $input = <STDIN>); |
126 | chomp (my $input = <STDIN>); |
85 | warn "read: $input\n"; |
127 | warn "read: $input\n"; |
86 | undef $w; |
128 | undef $w; |
87 | }); |
129 | }); |
88 | |
130 | |
89 | TIMER WATCHERS |
131 | TIME WATCHERS |
90 | You can create a timer watcher by calling the "AnyEvent->timer" method |
132 | You can create a time watcher by calling the "AnyEvent->timer" method |
91 | with the following mandatory arguments: |
133 | with the following mandatory arguments: |
92 | |
134 | |
93 | "after" after how many seconds (fractions are supported) should the |
135 | "after" after how many seconds (fractions are supported) should the |
94 | timer activate. "cb" the callback to invoke. |
136 | timer activate. "cb" the callback to invoke. |
95 | |
137 | |
… | |
… | |
103 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
145 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
104 | warn "timeout\n"; |
146 | warn "timeout\n"; |
105 | }); |
147 | }); |
106 | |
148 | |
107 | # to cancel the timer: |
149 | # to cancel the timer: |
108 | undef $w |
150 | undef $w; |
109 | |
151 | |
110 | CONDITION WATCHERS |
152 | CONDITION WATCHERS |
111 | Condition watchers can be created by calling the "AnyEvent->condvar" |
153 | Condition watchers can be created by calling the "AnyEvent->condvar" |
112 | method without any arguments. |
154 | method without any arguments. |
113 | |
155 | |
114 | A condition watcher watches for a condition - precisely that the |
156 | A condition watcher watches for a condition - precisely that the |
115 | "->broadcast" method has been called. |
157 | "->broadcast" method has been called. |
|
|
158 | |
|
|
159 | Note that condition watchers recurse into the event loop - if you have |
|
|
160 | two watchers that call "->wait" in a round-robbin fashion, you lose. |
|
|
161 | Therefore, condition watchers are good to export to your caller, but you |
|
|
162 | should avoid making a blocking wait, at least in callbacks, as this |
|
|
163 | usually asks for trouble. |
116 | |
164 | |
117 | The watcher has only two methods: |
165 | The watcher has only two methods: |
118 | |
166 | |
119 | $cv->wait |
167 | $cv->wait |
120 | Wait (blocking if necessary) until the "->broadcast" method has been |
168 | Wait (blocking if necessary) until the "->broadcast" method has been |
… | |
… | |
145 | # or socket watcher the calls $result_ready->broadcast |
193 | # or socket watcher the calls $result_ready->broadcast |
146 | # when the "result" is ready. |
194 | # when the "result" is ready. |
147 | |
195 | |
148 | $result_ready->wait; |
196 | $result_ready->wait; |
149 | |
197 | |
|
|
198 | SIGNAL WATCHERS |
|
|
199 | You can listen for signals using a signal watcher, "signal" is the |
|
|
200 | signal *name* without any "SIG" prefix. Multiple signals events can be |
|
|
201 | clumped together into one callback invocation, and callback invocation |
|
|
202 | might or might not be asynchronous. |
|
|
203 | |
|
|
204 | These watchers might use %SIG, so programs overwriting those signals |
|
|
205 | directly will likely not work correctly. |
|
|
206 | |
|
|
207 | Example: exit on SIGINT |
|
|
208 | |
|
|
209 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
210 | |
|
|
211 | CHILD PROCESS WATCHERS |
|
|
212 | You can also listen for the status of a child process specified by the |
|
|
213 | "pid" argument (or any child if the pid argument is 0). The watcher will |
|
|
214 | trigger as often as status change for the child are received. This works |
|
|
215 | by installing a signal handler for "SIGCHLD". The callback will be |
|
|
216 | called with the pid and exit status (as returned by waitpid). |
|
|
217 | |
|
|
218 | Example: wait for pid 1333 |
|
|
219 | |
|
|
220 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
|
|
221 | |
150 | GLOBALS |
222 | GLOBALS |
151 | $AnyEvent::MODEL |
223 | $AnyEvent::MODEL |
152 | Contains "undef" until the first watcher is being created. Then it |
224 | Contains "undef" until the first watcher is being created. Then it |
153 | contains the event model that is being used, which is the name of |
225 | contains the event model that is being used, which is the name of |
154 | the Perl class implementing the model. This class is usually one of |
226 | the Perl class implementing the model. This class is usually one of |
155 | the "AnyEvent::Impl:xxx" modules, but can be any other class in the |
227 | the "AnyEvent::Impl:xxx" modules, but can be any other class in the |
156 | case AnyEvent has been extended at runtime (e.g. in *rxvt-unicode*). |
228 | case AnyEvent has been extended at runtime (e.g. in *rxvt-unicode*). |
157 | |
229 | |
158 | The known classes so far are: |
230 | The known classes so far are: |
159 | |
231 | |
160 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
232 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
|
|
233 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
|
|
234 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
161 | AnyEvent::Impl::Event based on Event, also best choice :) |
235 | AnyEvent::Impl::Event based on Event, also second best choice :) |
162 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
236 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
163 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
237 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
164 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
238 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
|
|
239 | |
|
|
240 | AnyEvent::detect |
|
|
241 | Returns $AnyEvent::MODEL, forcing autodetection of the event model |
|
|
242 | if necessary. You should only call this function right before you |
|
|
243 | would have created an AnyEvent watcher anyway, that is, very late at |
|
|
244 | runtime. |
165 | |
245 | |
166 | WHAT TO DO IN A MODULE |
246 | WHAT TO DO IN A MODULE |
167 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
247 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
168 | freely, but you should not load a specific event module or rely on it. |
248 | freely, but you should not load a specific event module or rely on it. |
169 | |
249 | |
… | |
… | |
207 | This tells AnyEvent to (literally) use the "urxvt::anyevent::" |
287 | This tells AnyEvent to (literally) use the "urxvt::anyevent::" |
208 | package/class when it finds the "urxvt" package/module is loaded. When |
288 | package/class when it finds the "urxvt" package/module is loaded. When |
209 | AnyEvent is loaded and asked to find a suitable event model, it will |
289 | AnyEvent is loaded and asked to find a suitable event model, it will |
210 | first check for the presence of urxvt. |
290 | first check for the presence of urxvt. |
211 | |
291 | |
212 | The class should prove implementations for all watcher types (see |
292 | The class should provide implementations for all watcher types (see |
213 | AnyEvent::Impl::Event (source code), AnyEvent::Impl::Glib (Source code) |
293 | AnyEvent::Impl::Event (source code), AnyEvent::Impl::Glib (Source code) |
214 | and so on for actual examples, use "perldoc -m AnyEvent::Impl::Glib" to |
294 | and so on for actual examples, use "perldoc -m AnyEvent::Impl::Glib" to |
215 | see the sources). |
295 | see the sources). |
216 | |
296 | |
217 | The above isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt) uses the |
297 | The above isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt) uses the |
… | |
… | |
221 | *rxvt-unicode* distribution. |
301 | *rxvt-unicode* distribution. |
222 | |
302 | |
223 | *rxvt-unicode* also cheats a bit by not providing blocking access to |
303 | *rxvt-unicode* also cheats a bit by not providing blocking access to |
224 | condition variables: code blocking while waiting for a condition will |
304 | condition variables: code blocking while waiting for a condition will |
225 | "die". This still works with most modules/usages, and blocking calls |
305 | "die". This still works with most modules/usages, and blocking calls |
226 | must not be in an interactive appliation, so it makes sense. |
306 | must not be in an interactive application, so it makes sense. |
227 | |
307 | |
228 | ENVIRONMENT VARIABLES |
308 | ENVIRONMENT VARIABLES |
229 | The following environment variables are used by this module: |
309 | The following environment variables are used by this module: |
230 | |
310 | |
231 | "PERL_ANYEVENT_VERBOSE" when set to 2 or higher, reports which event |
311 | "PERL_ANYEVENT_VERBOSE" when set to 2 or higher, reports which event |