… | |
… | |
7 | |
7 | |
8 | =head1 SYNOPSIS |
8 | =head1 SYNOPSIS |
9 | |
9 | |
10 | use AnyEvent; |
10 | use AnyEvent; |
11 | |
11 | |
|
|
12 | # if you prefer function calls, look at the AE manpage for |
|
|
13 | # an alternative API. |
|
|
14 | |
12 | # file descriptor readable |
15 | # file handle or descriptor readable |
13 | my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); |
16 | my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); |
14 | |
17 | |
15 | # one-shot or repeating timers |
18 | # one-shot or repeating timers |
16 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
19 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
17 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
20 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ...); |
18 | |
21 | |
19 | print AnyEvent->now; # prints current event loop time |
22 | print AnyEvent->now; # prints current event loop time |
20 | print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. |
23 | print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. |
21 | |
24 | |
22 | # POSIX signal |
25 | # POSIX signal |
… | |
… | |
43 | in a tutorial or some gentle introduction, have a look at the |
46 | in a tutorial or some gentle introduction, have a look at the |
44 | L<AnyEvent::Intro> manpage. |
47 | L<AnyEvent::Intro> manpage. |
45 | |
48 | |
46 | =head1 SUPPORT |
49 | =head1 SUPPORT |
47 | |
50 | |
|
|
51 | An FAQ document is available as L<AnyEvent::FAQ>. |
|
|
52 | |
48 | There is a mailinglist for discussing all things AnyEvent, and an IRC |
53 | There also is a mailinglist for discussing all things AnyEvent, and an IRC |
49 | channel, too. |
54 | channel, too. |
50 | |
55 | |
51 | See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software |
56 | See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software |
52 | Repository>, at L<http://anyevent.schmorp.de>, for more info. |
57 | Repository>, at L<http://anyevent.schmorp.de>, for more info. |
53 | |
58 | |
… | |
… | |
73 | module users into the same thing by forcing them to use the same event |
78 | module users into the same thing by forcing them to use the same event |
74 | model you use. |
79 | model you use. |
75 | |
80 | |
76 | For modules like POE or IO::Async (which is a total misnomer as it is |
81 | For modules like POE or IO::Async (which is a total misnomer as it is |
77 | actually doing all I/O I<synchronously>...), using them in your module is |
82 | actually doing all I/O I<synchronously>...), using them in your module is |
78 | like joining a cult: After you joined, you are dependent on them and you |
83 | like joining a cult: After you join, you are dependent on them and you |
79 | cannot use anything else, as they are simply incompatible to everything |
84 | cannot use anything else, as they are simply incompatible to everything |
80 | that isn't them. What's worse, all the potential users of your |
85 | that isn't them. What's worse, all the potential users of your |
81 | module are I<also> forced to use the same event loop you use. |
86 | module are I<also> forced to use the same event loop you use. |
82 | |
87 | |
83 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
88 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
84 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
89 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
85 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
90 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
86 | your module uses one of those, every user of your module has to use it, |
91 | your module uses one of those, every user of your module has to use it, |
87 | too. But if your module uses AnyEvent, it works transparently with all |
92 | too. But if your module uses AnyEvent, it works transparently with all |
88 | event models it supports (including stuff like IO::Async, as long as those |
93 | event models it supports (including stuff like IO::Async, as long as those |
89 | use one of the supported event loops. It is trivial to add new event loops |
94 | use one of the supported event loops. It is easy to add new event loops |
90 | to AnyEvent, too, so it is future-proof). |
95 | to AnyEvent, too, so it is future-proof). |
91 | |
96 | |
92 | In addition to being free of having to use I<the one and only true event |
97 | In addition to being free of having to use I<the one and only true event |
93 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
98 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
94 | modules, you get an enormous amount of code and strict rules you have to |
99 | modules, you get an enormous amount of code and strict rules you have to |
95 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
100 | follow. AnyEvent, on the other hand, is lean and to the point, by only |
96 | offering the functionality that is necessary, in as thin as a wrapper as |
101 | offering the functionality that is necessary, in as thin as a wrapper as |
97 | technically possible. |
102 | technically possible. |
98 | |
103 | |
99 | Of course, AnyEvent comes with a big (and fully optional!) toolbox |
104 | Of course, AnyEvent comes with a big (and fully optional!) toolbox |
100 | of useful functionality, such as an asynchronous DNS resolver, 100% |
105 | of useful functionality, such as an asynchronous DNS resolver, 100% |
… | |
… | |
106 | useful) and you want to force your users to use the one and only event |
111 | useful) and you want to force your users to use the one and only event |
107 | model, you should I<not> use this module. |
112 | model, you should I<not> use this module. |
108 | |
113 | |
109 | =head1 DESCRIPTION |
114 | =head1 DESCRIPTION |
110 | |
115 | |
111 | L<AnyEvent> provides an identical interface to multiple event loops. This |
116 | L<AnyEvent> provides a uniform interface to various event loops. This |
112 | allows module authors to utilise an event loop without forcing module |
117 | allows module authors to use event loop functionality without forcing |
113 | users to use the same event loop (as only a single event loop can coexist |
118 | module users to use a specific event loop implementation (since more |
114 | peacefully at any one time). |
119 | than one event loop cannot coexist peacefully). |
115 | |
120 | |
116 | The interface itself is vaguely similar, but not identical to the L<Event> |
121 | The interface itself is vaguely similar, but not identical to the L<Event> |
117 | module. |
122 | module. |
118 | |
123 | |
119 | During the first call of any watcher-creation method, the module tries |
124 | During the first call of any watcher-creation method, the module tries |
120 | to detect the currently loaded event loop by probing whether one of the |
125 | to detect the currently loaded event loop by probing whether one of the |
121 | following modules is already loaded: L<EV>, |
126 | following modules is already loaded: L<EV>, L<AnyEvent::Impl::Perl>, |
122 | L<Event>, L<Glib>, L<AnyEvent::Impl::Perl>, L<Tk>, L<Event::Lib>, L<Qt>, |
127 | L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. The first one |
123 | L<POE>. The first one found is used. If none are found, the module tries |
128 | found is used. If none are detected, the module tries to load the first |
124 | to load these modules (excluding Tk, Event::Lib, Qt and POE as the pure perl |
129 | four modules in the order given; but note that if L<EV> is not |
125 | adaptor should always succeed) in the order given. The first one that can |
130 | available, the pure-perl L<AnyEvent::Impl::Perl> should always work, so |
126 | be successfully loaded will be used. If, after this, still none could be |
131 | the other two are not normally tried. |
127 | found, AnyEvent will fall back to a pure-perl event loop, which is not |
|
|
128 | very efficient, but should work everywhere. |
|
|
129 | |
132 | |
130 | Because AnyEvent first checks for modules that are already loaded, loading |
133 | Because AnyEvent first checks for modules that are already loaded, loading |
131 | an event model explicitly before first using AnyEvent will likely make |
134 | an event model explicitly before first using AnyEvent will likely make |
132 | that model the default. For example: |
135 | that model the default. For example: |
133 | |
136 | |
… | |
… | |
135 | use AnyEvent; |
138 | use AnyEvent; |
136 | |
139 | |
137 | # .. AnyEvent will likely default to Tk |
140 | # .. AnyEvent will likely default to Tk |
138 | |
141 | |
139 | The I<likely> means that, if any module loads another event model and |
142 | The I<likely> means that, if any module loads another event model and |
140 | starts using it, all bets are off. Maybe you should tell their authors to |
143 | starts using it, all bets are off - this case should be very rare though, |
141 | use AnyEvent so their modules work together with others seamlessly... |
144 | as very few modules hardcode event loops without announcing this very |
|
|
145 | loudly. |
142 | |
146 | |
143 | The pure-perl implementation of AnyEvent is called |
147 | The pure-perl implementation of AnyEvent is called |
144 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
148 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
145 | explicitly and enjoy the high availability of that event loop :) |
149 | explicitly and enjoy the high availability of that event loop :) |
146 | |
150 | |
… | |
… | |
155 | callback when the event occurs (of course, only when the event model |
159 | callback when the event occurs (of course, only when the event model |
156 | is in control). |
160 | is in control). |
157 | |
161 | |
158 | Note that B<callbacks must not permanently change global variables> |
162 | Note that B<callbacks must not permanently change global variables> |
159 | potentially in use by the event loop (such as C<$_> or C<$[>) and that B<< |
163 | potentially in use by the event loop (such as C<$_> or C<$[>) and that B<< |
160 | callbacks must not C<die> >>. The former is good programming practise in |
164 | callbacks must not C<die> >>. The former is good programming practice in |
161 | Perl and the latter stems from the fact that exception handling differs |
165 | Perl and the latter stems from the fact that exception handling differs |
162 | widely between event loops. |
166 | widely between event loops. |
163 | |
167 | |
164 | To disable the watcher you have to destroy it (e.g. by setting the |
168 | To disable a watcher you have to destroy it (e.g. by setting the |
165 | variable you store it in to C<undef> or otherwise deleting all references |
169 | variable you store it in to C<undef> or otherwise deleting all references |
166 | to it). |
170 | to it). |
167 | |
171 | |
168 | All watchers are created by calling a method on the C<AnyEvent> class. |
172 | All watchers are created by calling a method on the C<AnyEvent> class. |
169 | |
173 | |
170 | Many watchers either are used with "recursion" (repeating timers for |
174 | Many watchers either are used with "recursion" (repeating timers for |
171 | example), or need to refer to their watcher object in other ways. |
175 | example), or need to refer to their watcher object in other ways. |
172 | |
176 | |
173 | An any way to achieve that is this pattern: |
177 | One way to achieve that is this pattern: |
174 | |
178 | |
175 | my $w; $w = AnyEvent->type (arg => value ..., cb => sub { |
179 | my $w; $w = AnyEvent->type (arg => value ..., cb => sub { |
176 | # you can use $w here, for example to undef it |
180 | # you can use $w here, for example to undef it |
177 | undef $w; |
181 | undef $w; |
178 | }); |
182 | }); |
… | |
… | |
180 | Note that C<my $w; $w => combination. This is necessary because in Perl, |
184 | Note that C<my $w; $w => combination. This is necessary because in Perl, |
181 | my variables are only visible after the statement in which they are |
185 | my variables are only visible after the statement in which they are |
182 | declared. |
186 | declared. |
183 | |
187 | |
184 | =head2 I/O WATCHERS |
188 | =head2 I/O WATCHERS |
|
|
189 | |
|
|
190 | $w = AnyEvent->io ( |
|
|
191 | fh => <filehandle_or_fileno>, |
|
|
192 | poll => <"r" or "w">, |
|
|
193 | cb => <callback>, |
|
|
194 | ); |
185 | |
195 | |
186 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
196 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
187 | with the following mandatory key-value pairs as arguments: |
197 | with the following mandatory key-value pairs as arguments: |
188 | |
198 | |
189 | C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch |
199 | C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch |
… | |
… | |
204 | |
214 | |
205 | The I/O watcher might use the underlying file descriptor or a copy of it. |
215 | The I/O watcher might use the underlying file descriptor or a copy of it. |
206 | You must not close a file handle as long as any watcher is active on the |
216 | You must not close a file handle as long as any watcher is active on the |
207 | underlying file descriptor. |
217 | underlying file descriptor. |
208 | |
218 | |
209 | Some event loops issue spurious readyness notifications, so you should |
219 | Some event loops issue spurious readiness notifications, so you should |
210 | always use non-blocking calls when reading/writing from/to your file |
220 | always use non-blocking calls when reading/writing from/to your file |
211 | handles. |
221 | handles. |
212 | |
222 | |
213 | Example: wait for readability of STDIN, then read a line and disable the |
223 | Example: wait for readability of STDIN, then read a line and disable the |
214 | watcher. |
224 | watcher. |
… | |
… | |
219 | undef $w; |
229 | undef $w; |
220 | }); |
230 | }); |
221 | |
231 | |
222 | =head2 TIME WATCHERS |
232 | =head2 TIME WATCHERS |
223 | |
233 | |
|
|
234 | $w = AnyEvent->timer (after => <seconds>, cb => <callback>); |
|
|
235 | |
|
|
236 | $w = AnyEvent->timer ( |
|
|
237 | after => <fractional_seconds>, |
|
|
238 | interval => <fractional_seconds>, |
|
|
239 | cb => <callback>, |
|
|
240 | ); |
|
|
241 | |
224 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
242 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
225 | method with the following mandatory arguments: |
243 | method with the following mandatory arguments: |
226 | |
244 | |
227 | C<after> specifies after how many seconds (fractional values are |
245 | C<after> specifies after how many seconds (fractional values are |
228 | supported) the callback should be invoked. C<cb> is the callback to invoke |
246 | supported) the callback should be invoked. C<cb> is the callback to invoke |
… | |
… | |
230 | |
248 | |
231 | Although the callback might get passed parameters, their value and |
249 | Although the callback might get passed parameters, their value and |
232 | presence is undefined and you cannot rely on them. Portable AnyEvent |
250 | presence is undefined and you cannot rely on them. Portable AnyEvent |
233 | callbacks cannot use arguments passed to time watcher callbacks. |
251 | callbacks cannot use arguments passed to time watcher callbacks. |
234 | |
252 | |
235 | The callback will normally be invoked once only. If you specify another |
253 | The callback will normally be invoked only once. If you specify another |
236 | parameter, C<interval>, as a strictly positive number (> 0), then the |
254 | parameter, C<interval>, as a strictly positive number (> 0), then the |
237 | callback will be invoked regularly at that interval (in fractional |
255 | callback will be invoked regularly at that interval (in fractional |
238 | seconds) after the first invocation. If C<interval> is specified with a |
256 | seconds) after the first invocation. If C<interval> is specified with a |
239 | false value, then it is treated as if it were missing. |
257 | false value, then it is treated as if it were not specified at all. |
240 | |
258 | |
241 | The callback will be rescheduled before invoking the callback, but no |
259 | The callback will be rescheduled before invoking the callback, but no |
242 | attempt is done to avoid timer drift in most backends, so the interval is |
260 | attempt is made to avoid timer drift in most backends, so the interval is |
243 | only approximate. |
261 | only approximate. |
244 | |
262 | |
245 | Example: fire an event after 7.7 seconds. |
263 | Example: fire an event after 7.7 seconds. |
246 | |
264 | |
247 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
265 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
… | |
… | |
265 | |
283 | |
266 | While most event loops expect timers to specified in a relative way, they |
284 | While most event loops expect timers to specified in a relative way, they |
267 | use absolute time internally. This makes a difference when your clock |
285 | use absolute time internally. This makes a difference when your clock |
268 | "jumps", for example, when ntp decides to set your clock backwards from |
286 | "jumps", for example, when ntp decides to set your clock backwards from |
269 | the wrong date of 2014-01-01 to 2008-01-01, a watcher that is supposed to |
287 | the wrong date of 2014-01-01 to 2008-01-01, a watcher that is supposed to |
270 | fire "after" a second might actually take six years to finally fire. |
288 | fire "after a second" might actually take six years to finally fire. |
271 | |
289 | |
272 | AnyEvent cannot compensate for this. The only event loop that is conscious |
290 | AnyEvent cannot compensate for this. The only event loop that is conscious |
273 | about these issues is L<EV>, which offers both relative (ev_timer, based |
291 | of these issues is L<EV>, which offers both relative (ev_timer, based |
274 | on true relative time) and absolute (ev_periodic, based on wallclock time) |
292 | on true relative time) and absolute (ev_periodic, based on wallclock time) |
275 | timers. |
293 | timers. |
276 | |
294 | |
277 | AnyEvent always prefers relative timers, if available, matching the |
295 | AnyEvent always prefers relative timers, if available, matching the |
278 | AnyEvent API. |
296 | AnyEvent API. |
… | |
… | |
300 | I<In almost all cases (in all cases if you don't care), this is the |
318 | I<In almost all cases (in all cases if you don't care), this is the |
301 | function to call when you want to know the current time.> |
319 | function to call when you want to know the current time.> |
302 | |
320 | |
303 | This function is also often faster then C<< AnyEvent->time >>, and |
321 | This function is also often faster then C<< AnyEvent->time >>, and |
304 | thus the preferred method if you want some timestamp (for example, |
322 | thus the preferred method if you want some timestamp (for example, |
305 | L<AnyEvent::Handle> uses this to update it's activity timeouts). |
323 | L<AnyEvent::Handle> uses this to update its activity timeouts). |
306 | |
324 | |
307 | The rest of this section is only of relevance if you try to be very exact |
325 | The rest of this section is only of relevance if you try to be very exact |
308 | with your timing, you can skip it without bad conscience. |
326 | with your timing; you can skip it without a bad conscience. |
309 | |
327 | |
310 | For a practical example of when these times differ, consider L<Event::Lib> |
328 | For a practical example of when these times differ, consider L<Event::Lib> |
311 | and L<EV> and the following set-up: |
329 | and L<EV> and the following set-up: |
312 | |
330 | |
313 | The event loop is running and has just invoked one of your callback at |
331 | The event loop is running and has just invoked one of your callbacks at |
314 | time=500 (assume no other callbacks delay processing). In your callback, |
332 | time=500 (assume no other callbacks delay processing). In your callback, |
315 | you wait a second by executing C<sleep 1> (blocking the process for a |
333 | you wait a second by executing C<sleep 1> (blocking the process for a |
316 | second) and then (at time=501) you create a relative timer that fires |
334 | second) and then (at time=501) you create a relative timer that fires |
317 | after three seconds. |
335 | after three seconds. |
318 | |
336 | |
… | |
… | |
349 | might affect timers and time-outs. |
367 | might affect timers and time-outs. |
350 | |
368 | |
351 | When this is the case, you can call this method, which will update the |
369 | When this is the case, you can call this method, which will update the |
352 | event loop's idea of "current time". |
370 | event loop's idea of "current time". |
353 | |
371 | |
|
|
372 | A typical example would be a script in a web server (e.g. C<mod_perl>) - |
|
|
373 | when mod_perl executes the script, then the event loop will have the wrong |
|
|
374 | idea about the "current time" (being potentially far in the past, when the |
|
|
375 | script ran the last time). In that case you should arrange a call to C<< |
|
|
376 | AnyEvent->now_update >> each time the web server process wakes up again |
|
|
377 | (e.g. at the start of your script, or in a handler). |
|
|
378 | |
354 | Note that updating the time I<might> cause some events to be handled. |
379 | Note that updating the time I<might> cause some events to be handled. |
355 | |
380 | |
356 | =back |
381 | =back |
357 | |
382 | |
358 | =head2 SIGNAL WATCHERS |
383 | =head2 SIGNAL WATCHERS |
|
|
384 | |
|
|
385 | $w = AnyEvent->signal (signal => <uppercase_signal_name>, cb => <callback>); |
359 | |
386 | |
360 | You can watch for signals using a signal watcher, C<signal> is the signal |
387 | You can watch for signals using a signal watcher, C<signal> is the signal |
361 | I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl |
388 | I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl |
362 | callback to be invoked whenever a signal occurs. |
389 | callback to be invoked whenever a signal occurs. |
363 | |
390 | |
… | |
… | |
380 | |
407 | |
381 | Example: exit on SIGINT |
408 | Example: exit on SIGINT |
382 | |
409 | |
383 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
410 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
384 | |
411 | |
|
|
412 | =head3 Restart Behaviour |
|
|
413 | |
|
|
414 | While restart behaviour is up to the event loop implementation, most will |
|
|
415 | not restart syscalls (that includes L<Async::Interrupt> and AnyEvent's |
|
|
416 | pure perl implementation). |
|
|
417 | |
|
|
418 | =head3 Safe/Unsafe Signals |
|
|
419 | |
|
|
420 | Perl signals can be either "safe" (synchronous to opcode handling) or |
|
|
421 | "unsafe" (asynchronous) - the former might get delayed indefinitely, the |
|
|
422 | latter might corrupt your memory. |
|
|
423 | |
|
|
424 | AnyEvent signal handlers are, in addition, synchronous to the event loop, |
|
|
425 | i.e. they will not interrupt your running perl program but will only be |
|
|
426 | called as part of the normal event handling (just like timer, I/O etc. |
|
|
427 | callbacks, too). |
|
|
428 | |
385 | =head3 Signal Races, Delays and Workarounds |
429 | =head3 Signal Races, Delays and Workarounds |
386 | |
430 | |
387 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
431 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
388 | callbacks to signals in a generic way, which is a pity, as you cannot do |
432 | callbacks to signals in a generic way, which is a pity, as you cannot |
389 | race-free signal handling in perl. AnyEvent will try to do it's best, but |
433 | do race-free signal handling in perl, requiring C libraries for |
|
|
434 | this. AnyEvent will try to do its best, which means in some cases, |
390 | in some cases, signals will be delayed. The maximum time a signal might |
435 | signals will be delayed. The maximum time a signal might be delayed is |
391 | be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 |
436 | specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 seconds). This |
392 | seconds). This variable can be changed only before the first signal |
437 | variable can be changed only before the first signal watcher is created, |
393 | watcher is created, and should be left alone otherwise. Higher values |
438 | and should be left alone otherwise. This variable determines how often |
|
|
439 | AnyEvent polls for signals (in case a wake-up was missed). Higher values |
394 | will cause fewer spurious wake-ups, which is better for power and CPU |
440 | will cause fewer spurious wake-ups, which is better for power and CPU |
|
|
441 | saving. |
|
|
442 | |
395 | saving. All these problems can be avoided by installing the optional |
443 | All these problems can be avoided by installing the optional |
396 | L<Async::Interrupt> module. This will not work with inherently broken |
444 | L<Async::Interrupt> module, which works with most event loops. It will not |
397 | event loops such as L<Event> or L<Event::Lib> (and not with L<POE> |
445 | work with inherently broken event loops such as L<Event> or L<Event::Lib> |
398 | currently, as POE does it's own workaround with one-second latency). With |
446 | (and not with L<POE> currently, as POE does its own workaround with |
399 | those, you just have to suffer the delays. |
447 | one-second latency). For those, you just have to suffer the delays. |
400 | |
448 | |
401 | =head2 CHILD PROCESS WATCHERS |
449 | =head2 CHILD PROCESS WATCHERS |
402 | |
450 | |
|
|
451 | $w = AnyEvent->child (pid => <process id>, cb => <callback>); |
|
|
452 | |
403 | You can also watch on a child process exit and catch its exit status. |
453 | You can also watch for a child process exit and catch its exit status. |
404 | |
454 | |
405 | The child process is specified by the C<pid> argument (one some backends, |
455 | The child process is specified by the C<pid> argument (on some backends, |
406 | using C<0> watches for any child process exit, on others this will |
456 | using C<0> watches for any child process exit, on others this will |
407 | croak). The watcher will be triggered only when the child process has |
457 | croak). The watcher will be triggered only when the child process has |
408 | finished and an exit status is available, not on any trace events |
458 | finished and an exit status is available, not on any trace events |
409 | (stopped/continued). |
459 | (stopped/continued). |
410 | |
460 | |
… | |
… | |
455 | # do something else, then wait for process exit |
505 | # do something else, then wait for process exit |
456 | $done->recv; |
506 | $done->recv; |
457 | |
507 | |
458 | =head2 IDLE WATCHERS |
508 | =head2 IDLE WATCHERS |
459 | |
509 | |
460 | Sometimes there is a need to do something, but it is not so important |
510 | $w = AnyEvent->idle (cb => <callback>); |
461 | to do it instantly, but only when there is nothing better to do. This |
|
|
462 | "nothing better to do" is usually defined to be "no other events need |
|
|
463 | attention by the event loop". |
|
|
464 | |
511 | |
465 | Idle watchers ideally get invoked when the event loop has nothing |
512 | This will repeatedly invoke the callback after the process becomes idle, |
466 | better to do, just before it would block the process to wait for new |
513 | until either the watcher is destroyed or new events have been detected. |
467 | events. Instead of blocking, the idle watcher is invoked. |
|
|
468 | |
514 | |
469 | Most event loops unfortunately do not really support idle watchers (only |
515 | Idle watchers are useful when there is a need to do something, but it |
|
|
516 | is not so important (or wise) to do it instantly. The callback will be |
|
|
517 | invoked only when there is "nothing better to do", which is usually |
|
|
518 | defined as "all outstanding events have been handled and no new events |
|
|
519 | have been detected". That means that idle watchers ideally get invoked |
|
|
520 | when the event loop has just polled for new events but none have been |
|
|
521 | detected. Instead of blocking to wait for more events, the idle watchers |
|
|
522 | will be invoked. |
|
|
523 | |
|
|
524 | Unfortunately, most event loops do not really support idle watchers (only |
470 | EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent |
525 | EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent |
471 | will simply call the callback "from time to time". |
526 | will simply call the callback "from time to time". |
472 | |
527 | |
473 | Example: read lines from STDIN, but only process them when the |
528 | Example: read lines from STDIN, but only process them when the |
474 | program is otherwise idle: |
529 | program is otherwise idle: |
… | |
… | |
490 | }); |
545 | }); |
491 | }); |
546 | }); |
492 | |
547 | |
493 | =head2 CONDITION VARIABLES |
548 | =head2 CONDITION VARIABLES |
494 | |
549 | |
|
|
550 | $cv = AnyEvent->condvar; |
|
|
551 | |
|
|
552 | $cv->send (<list>); |
|
|
553 | my @res = $cv->recv; |
|
|
554 | |
495 | If you are familiar with some event loops you will know that all of them |
555 | If you are familiar with some event loops you will know that all of them |
496 | require you to run some blocking "loop", "run" or similar function that |
556 | require you to run some blocking "loop", "run" or similar function that |
497 | will actively watch for new events and call your callbacks. |
557 | will actively watch for new events and call your callbacks. |
498 | |
558 | |
499 | AnyEvent is slightly different: it expects somebody else to run the event |
559 | AnyEvent is slightly different: it expects somebody else to run the event |
500 | loop and will only block when necessary (usually when told by the user). |
560 | loop and will only block when necessary (usually when told by the user). |
501 | |
561 | |
502 | The instrument to do that is called a "condition variable", so called |
562 | The tool to do that is called a "condition variable", so called because |
503 | because they represent a condition that must become true. |
563 | they represent a condition that must become true. |
504 | |
564 | |
505 | Now is probably a good time to look at the examples further below. |
565 | Now is probably a good time to look at the examples further below. |
506 | |
566 | |
507 | Condition variables can be created by calling the C<< AnyEvent->condvar |
567 | Condition variables can be created by calling the C<< AnyEvent->condvar |
508 | >> method, usually without arguments. The only argument pair allowed is |
568 | >> method, usually without arguments. The only argument pair allowed is |
… | |
… | |
513 | After creation, the condition variable is "false" until it becomes "true" |
573 | After creation, the condition variable is "false" until it becomes "true" |
514 | by calling the C<send> method (or calling the condition variable as if it |
574 | by calling the C<send> method (or calling the condition variable as if it |
515 | were a callback, read about the caveats in the description for the C<< |
575 | were a callback, read about the caveats in the description for the C<< |
516 | ->send >> method). |
576 | ->send >> method). |
517 | |
577 | |
518 | Condition variables are similar to callbacks, except that you can |
578 | Since condition variables are the most complex part of the AnyEvent API, here are |
519 | optionally wait for them. They can also be called merge points - points |
579 | some different mental models of what they are - pick the ones you can connect to: |
520 | in time where multiple outstanding events have been processed. And yet |
580 | |
521 | another way to call them is transactions - each condition variable can be |
581 | =over 4 |
522 | used to represent a transaction, which finishes at some point and delivers |
582 | |
523 | a result. And yet some people know them as "futures" - a promise to |
583 | =item * Condition variables are like callbacks - you can call them (and pass them instead |
524 | compute/deliver something that you can wait for. |
584 | of callbacks). Unlike callbacks however, you can also wait for them to be called. |
|
|
585 | |
|
|
586 | =item * Condition variables are signals - one side can emit or send them, |
|
|
587 | the other side can wait for them, or install a handler that is called when |
|
|
588 | the signal fires. |
|
|
589 | |
|
|
590 | =item * Condition variables are like "Merge Points" - points in your program |
|
|
591 | where you merge multiple independent results/control flows into one. |
|
|
592 | |
|
|
593 | =item * Condition variables represent a transaction - functions that start |
|
|
594 | some kind of transaction can return them, leaving the caller the choice |
|
|
595 | between waiting in a blocking fashion, or setting a callback. |
|
|
596 | |
|
|
597 | =item * Condition variables represent future values, or promises to deliver |
|
|
598 | some result, long before the result is available. |
|
|
599 | |
|
|
600 | =back |
525 | |
601 | |
526 | Condition variables are very useful to signal that something has finished, |
602 | Condition variables are very useful to signal that something has finished, |
527 | for example, if you write a module that does asynchronous http requests, |
603 | for example, if you write a module that does asynchronous http requests, |
528 | then a condition variable would be the ideal candidate to signal the |
604 | then a condition variable would be the ideal candidate to signal the |
529 | availability of results. The user can either act when the callback is |
605 | availability of results. The user can either act when the callback is |
… | |
… | |
542 | |
618 | |
543 | Condition variables are represented by hash refs in perl, and the keys |
619 | Condition variables are represented by hash refs in perl, and the keys |
544 | used by AnyEvent itself are all named C<_ae_XXX> to make subclassing |
620 | used by AnyEvent itself are all named C<_ae_XXX> to make subclassing |
545 | easy (it is often useful to build your own transaction class on top of |
621 | easy (it is often useful to build your own transaction class on top of |
546 | AnyEvent). To subclass, use C<AnyEvent::CondVar> as base class and call |
622 | AnyEvent). To subclass, use C<AnyEvent::CondVar> as base class and call |
547 | it's C<new> method in your own C<new> method. |
623 | its C<new> method in your own C<new> method. |
548 | |
624 | |
549 | There are two "sides" to a condition variable - the "producer side" which |
625 | There are two "sides" to a condition variable - the "producer side" which |
550 | eventually calls C<< -> send >>, and the "consumer side", which waits |
626 | eventually calls C<< -> send >>, and the "consumer side", which waits |
551 | for the send to occur. |
627 | for the send to occur. |
552 | |
628 | |
553 | Example: wait for a timer. |
629 | Example: wait for a timer. |
554 | |
630 | |
555 | # wait till the result is ready |
631 | # condition: "wait till the timer is fired" |
556 | my $result_ready = AnyEvent->condvar; |
632 | my $timer_fired = AnyEvent->condvar; |
557 | |
633 | |
558 | # do something such as adding a timer |
634 | # create the timer - we could wait for, say |
559 | # or socket watcher the calls $result_ready->send |
635 | # a handle becomign ready, or even an |
560 | # when the "result" is ready. |
636 | # AnyEvent::HTTP request to finish, but |
561 | # in this case, we simply use a timer: |
637 | # in this case, we simply use a timer: |
562 | my $w = AnyEvent->timer ( |
638 | my $w = AnyEvent->timer ( |
563 | after => 1, |
639 | after => 1, |
564 | cb => sub { $result_ready->send }, |
640 | cb => sub { $timer_fired->send }, |
565 | ); |
641 | ); |
566 | |
642 | |
567 | # this "blocks" (while handling events) till the callback |
643 | # this "blocks" (while handling events) till the callback |
568 | # calls -<send |
644 | # calls ->send |
569 | $result_ready->recv; |
645 | $timer_fired->recv; |
570 | |
646 | |
571 | Example: wait for a timer, but take advantage of the fact that condition |
647 | Example: wait for a timer, but take advantage of the fact that condition |
572 | variables are also callable directly. |
648 | variables are also callable directly. |
573 | |
649 | |
574 | my $done = AnyEvent->condvar; |
650 | my $done = AnyEvent->condvar; |
… | |
… | |
617 | they were a code reference). Calling them directly is the same as calling |
693 | they were a code reference). Calling them directly is the same as calling |
618 | C<send>. |
694 | C<send>. |
619 | |
695 | |
620 | =item $cv->croak ($error) |
696 | =item $cv->croak ($error) |
621 | |
697 | |
622 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
698 | Similar to send, but causes all calls to C<< ->recv >> to invoke |
623 | C<Carp::croak> with the given error message/object/scalar. |
699 | C<Carp::croak> with the given error message/object/scalar. |
624 | |
700 | |
625 | This can be used to signal any errors to the condition variable |
701 | This can be used to signal any errors to the condition variable |
626 | user/consumer. Doing it this way instead of calling C<croak> directly |
702 | user/consumer. Doing it this way instead of calling C<croak> directly |
627 | delays the error detetcion, but has the overwhelmign advantage that it |
703 | delays the error detection, but has the overwhelming advantage that it |
628 | diagnoses the error at the place where the result is expected, and not |
704 | diagnoses the error at the place where the result is expected, and not |
629 | deep in some event clalback without connection to the actual code causing |
705 | deep in some event callback with no connection to the actual code causing |
630 | the problem. |
706 | the problem. |
631 | |
707 | |
632 | =item $cv->begin ([group callback]) |
708 | =item $cv->begin ([group callback]) |
633 | |
709 | |
634 | =item $cv->end |
710 | =item $cv->end |
… | |
… | |
637 | one. For example, a function that pings many hosts in parallel might want |
713 | one. For example, a function that pings many hosts in parallel might want |
638 | to use a condition variable for the whole process. |
714 | to use a condition variable for the whole process. |
639 | |
715 | |
640 | Every call to C<< ->begin >> will increment a counter, and every call to |
716 | Every call to C<< ->begin >> will increment a counter, and every call to |
641 | C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end |
717 | C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end |
642 | >>, the (last) callback passed to C<begin> will be executed. That callback |
718 | >>, the (last) callback passed to C<begin> will be executed, passing the |
643 | is I<supposed> to call C<< ->send >>, but that is not required. If no |
719 | condvar as first argument. That callback is I<supposed> to call C<< ->send |
644 | callback was set, C<send> will be called without any arguments. |
720 | >>, but that is not required. If no group callback was set, C<send> will |
|
|
721 | be called without any arguments. |
645 | |
722 | |
646 | You can think of C<< $cv->send >> giving you an OR condition (one call |
723 | You can think of C<< $cv->send >> giving you an OR condition (one call |
647 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
724 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
648 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
725 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
649 | |
726 | |
… | |
… | |
671 | one call to C<begin>, so the condvar waits for all calls to C<end> before |
748 | one call to C<begin>, so the condvar waits for all calls to C<end> before |
672 | sending. |
749 | sending. |
673 | |
750 | |
674 | The ping example mentioned above is slightly more complicated, as the |
751 | The ping example mentioned above is slightly more complicated, as the |
675 | there are results to be passwd back, and the number of tasks that are |
752 | there are results to be passwd back, and the number of tasks that are |
676 | begung can potentially be zero: |
753 | begun can potentially be zero: |
677 | |
754 | |
678 | my $cv = AnyEvent->condvar; |
755 | my $cv = AnyEvent->condvar; |
679 | |
756 | |
680 | my %result; |
757 | my %result; |
681 | $cv->begin (sub { $cv->send (\%result) }); |
758 | $cv->begin (sub { shift->send (\%result) }); |
682 | |
759 | |
683 | for my $host (@list_of_hosts) { |
760 | for my $host (@list_of_hosts) { |
684 | $cv->begin; |
761 | $cv->begin; |
685 | ping_host_then_call_callback $host, sub { |
762 | ping_host_then_call_callback $host, sub { |
686 | $result{$host} = ...; |
763 | $result{$host} = ...; |
… | |
… | |
702 | to be called once the counter reaches C<0>, and second, it ensures that |
779 | to be called once the counter reaches C<0>, and second, it ensures that |
703 | C<send> is called even when C<no> hosts are being pinged (the loop |
780 | C<send> is called even when C<no> hosts are being pinged (the loop |
704 | doesn't execute once). |
781 | doesn't execute once). |
705 | |
782 | |
706 | This is the general pattern when you "fan out" into multiple (but |
783 | This is the general pattern when you "fan out" into multiple (but |
707 | potentially none) subrequests: use an outer C<begin>/C<end> pair to set |
784 | potentially zero) subrequests: use an outer C<begin>/C<end> pair to set |
708 | the callback and ensure C<end> is called at least once, and then, for each |
785 | the callback and ensure C<end> is called at least once, and then, for each |
709 | subrequest you start, call C<begin> and for each subrequest you finish, |
786 | subrequest you start, call C<begin> and for each subrequest you finish, |
710 | call C<end>. |
787 | call C<end>. |
711 | |
788 | |
712 | =back |
789 | =back |
… | |
… | |
719 | =over 4 |
796 | =over 4 |
720 | |
797 | |
721 | =item $cv->recv |
798 | =item $cv->recv |
722 | |
799 | |
723 | Wait (blocking if necessary) until the C<< ->send >> or C<< ->croak |
800 | Wait (blocking if necessary) until the C<< ->send >> or C<< ->croak |
724 | >> methods have been called on c<$cv>, while servicing other watchers |
801 | >> methods have been called on C<$cv>, while servicing other watchers |
725 | normally. |
802 | normally. |
726 | |
803 | |
727 | You can only wait once on a condition - additional calls are valid but |
804 | You can only wait once on a condition - additional calls are valid but |
728 | will return immediately. |
805 | will return immediately. |
729 | |
806 | |
… | |
… | |
746 | caller decide whether the call will block or not (for example, by coupling |
823 | caller decide whether the call will block or not (for example, by coupling |
747 | condition variables with some kind of request results and supporting |
824 | condition variables with some kind of request results and supporting |
748 | callbacks so the caller knows that getting the result will not block, |
825 | callbacks so the caller knows that getting the result will not block, |
749 | while still supporting blocking waits if the caller so desires). |
826 | while still supporting blocking waits if the caller so desires). |
750 | |
827 | |
751 | You can ensure that C<< -recv >> never blocks by setting a callback and |
828 | You can ensure that C<< ->recv >> never blocks by setting a callback and |
752 | only calling C<< ->recv >> from within that callback (or at a later |
829 | only calling C<< ->recv >> from within that callback (or at a later |
753 | time). This will work even when the event loop does not support blocking |
830 | time). This will work even when the event loop does not support blocking |
754 | waits otherwise. |
831 | waits otherwise. |
755 | |
832 | |
756 | =item $bool = $cv->ready |
833 | =item $bool = $cv->ready |
… | |
… | |
762 | |
839 | |
763 | This is a mutator function that returns the callback set and optionally |
840 | This is a mutator function that returns the callback set and optionally |
764 | replaces it before doing so. |
841 | replaces it before doing so. |
765 | |
842 | |
766 | The callback will be called when the condition becomes "true", i.e. when |
843 | The callback will be called when the condition becomes "true", i.e. when |
767 | C<send> or C<croak> are called, with the only argument being the condition |
844 | C<send> or C<croak> are called, with the only argument being the |
768 | variable itself. Calling C<recv> inside the callback or at any later time |
845 | condition variable itself. If the condition is already true, the |
769 | is guaranteed not to block. |
846 | callback is called immediately when it is set. Calling C<recv> inside |
|
|
847 | the callback or at any later time is guaranteed not to block. |
770 | |
848 | |
771 | =back |
849 | =back |
772 | |
850 | |
773 | =head1 SUPPORTED EVENT LOOPS/BACKENDS |
851 | =head1 SUPPORTED EVENT LOOPS/BACKENDS |
774 | |
852 | |
… | |
… | |
777 | =over 4 |
855 | =over 4 |
778 | |
856 | |
779 | =item Backends that are autoprobed when no other event loop can be found. |
857 | =item Backends that are autoprobed when no other event loop can be found. |
780 | |
858 | |
781 | EV is the preferred backend when no other event loop seems to be in |
859 | EV is the preferred backend when no other event loop seems to be in |
782 | use. If EV is not installed, then AnyEvent will try Event, and, failing |
860 | use. If EV is not installed, then AnyEvent will fall back to its own |
783 | that, will fall back to its own pure-perl implementation, which is |
861 | pure-perl implementation, which is available everywhere as it comes with |
784 | available everywhere as it comes with AnyEvent itself. |
862 | AnyEvent itself. |
785 | |
863 | |
786 | AnyEvent::Impl::EV based on EV (interface to libev, best choice). |
864 | AnyEvent::Impl::EV based on EV (interface to libev, best choice). |
787 | AnyEvent::Impl::Event based on Event, very stable, few glitches. |
|
|
788 | AnyEvent::Impl::Perl pure-perl implementation, fast and portable. |
865 | AnyEvent::Impl::Perl pure-perl implementation, fast and portable. |
789 | |
866 | |
790 | =item Backends that are transparently being picked up when they are used. |
867 | =item Backends that are transparently being picked up when they are used. |
791 | |
868 | |
792 | These will be used when they are currently loaded when the first watcher |
869 | These will be used if they are already loaded when the first watcher |
793 | is created, in which case it is assumed that the application is using |
870 | is created, in which case it is assumed that the application is using |
794 | them. This means that AnyEvent will automatically pick the right backend |
871 | them. This means that AnyEvent will automatically pick the right backend |
795 | when the main program loads an event module before anything starts to |
872 | when the main program loads an event module before anything starts to |
796 | create watchers. Nothing special needs to be done by the main program. |
873 | create watchers. Nothing special needs to be done by the main program. |
797 | |
874 | |
|
|
875 | AnyEvent::Impl::Event based on Event, very stable, few glitches. |
798 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
876 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
799 | AnyEvent::Impl::Tk based on Tk, very broken. |
877 | AnyEvent::Impl::Tk based on Tk, very broken. |
800 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
878 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
801 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
879 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
802 | AnyEvent::Impl::Irssi used when running within irssi. |
880 | AnyEvent::Impl::Irssi used when running within irssi. |
… | |
… | |
812 | |
890 | |
813 | Support for IO::Async can only be partial, as it is too broken and |
891 | Support for IO::Async can only be partial, as it is too broken and |
814 | architecturally limited to even support the AnyEvent API. It also |
892 | architecturally limited to even support the AnyEvent API. It also |
815 | is the only event loop that needs the loop to be set explicitly, so |
893 | is the only event loop that needs the loop to be set explicitly, so |
816 | it can only be used by a main program knowing about AnyEvent. See |
894 | it can only be used by a main program knowing about AnyEvent. See |
817 | L<AnyEvent::Impl::Async> for the gory details. |
895 | L<AnyEvent::Impl::IOAsync> for the gory details. |
818 | |
896 | |
819 | AnyEvent::Impl::IOAsync based on IO::Async, cannot be autoprobed. |
897 | AnyEvent::Impl::IOAsync based on IO::Async, cannot be autoprobed. |
820 | |
898 | |
821 | =item Event loops that are indirectly supported via other backends. |
899 | =item Event loops that are indirectly supported via other backends. |
822 | |
900 | |
… | |
… | |
850 | Contains C<undef> until the first watcher is being created, before the |
928 | Contains C<undef> until the first watcher is being created, before the |
851 | backend has been autodetected. |
929 | backend has been autodetected. |
852 | |
930 | |
853 | Afterwards it contains the event model that is being used, which is the |
931 | Afterwards it contains the event model that is being used, which is the |
854 | name of the Perl class implementing the model. This class is usually one |
932 | name of the Perl class implementing the model. This class is usually one |
855 | of the C<AnyEvent::Impl:xxx> modules, but can be any other class in the |
933 | of the C<AnyEvent::Impl::xxx> modules, but can be any other class in the |
856 | case AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode> it |
934 | case AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode> it |
857 | will be C<urxvt::anyevent>). |
935 | will be C<urxvt::anyevent>). |
858 | |
936 | |
859 | =item AnyEvent::detect |
937 | =item AnyEvent::detect |
860 | |
938 | |
861 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
939 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
862 | if necessary. You should only call this function right before you would |
940 | if necessary. You should only call this function right before you would |
863 | have created an AnyEvent watcher anyway, that is, as late as possible at |
941 | have created an AnyEvent watcher anyway, that is, as late as possible at |
864 | runtime, and not e.g. while initialising of your module. |
942 | runtime, and not e.g. during initialisation of your module. |
865 | |
943 | |
866 | If you need to do some initialisation before AnyEvent watchers are |
944 | If you need to do some initialisation before AnyEvent watchers are |
867 | created, use C<post_detect>. |
945 | created, use C<post_detect>. |
868 | |
946 | |
869 | =item $guard = AnyEvent::post_detect { BLOCK } |
947 | =item $guard = AnyEvent::post_detect { BLOCK } |
870 | |
948 | |
871 | Arranges for the code block to be executed as soon as the event model is |
949 | Arranges for the code block to be executed as soon as the event model is |
872 | autodetected (or immediately if this has already happened). |
950 | autodetected (or immediately if that has already happened). |
873 | |
951 | |
874 | The block will be executed I<after> the actual backend has been detected |
952 | The block will be executed I<after> the actual backend has been detected |
875 | (C<$AnyEvent::MODEL> is set), but I<before> any watchers have been |
953 | (C<$AnyEvent::MODEL> is set), but I<before> any watchers have been |
876 | created, so it is possible to e.g. patch C<@AnyEvent::ISA> or do |
954 | created, so it is possible to e.g. patch C<@AnyEvent::ISA> or do |
877 | other initialisations - see the sources of L<AnyEvent::Strict> or |
955 | other initialisations - see the sources of L<AnyEvent::Strict> or |
… | |
… | |
886 | that automatically removes the callback again when it is destroyed (or |
964 | that automatically removes the callback again when it is destroyed (or |
887 | C<undef> when the hook was immediately executed). See L<AnyEvent::AIO> for |
965 | C<undef> when the hook was immediately executed). See L<AnyEvent::AIO> for |
888 | a case where this is useful. |
966 | a case where this is useful. |
889 | |
967 | |
890 | Example: Create a watcher for the IO::AIO module and store it in |
968 | Example: Create a watcher for the IO::AIO module and store it in |
891 | C<$WATCHER>. Only do so after the event loop is initialised, though. |
969 | C<$WATCHER>, but do so only do so after the event loop is initialised. |
892 | |
970 | |
893 | our WATCHER; |
971 | our WATCHER; |
894 | |
972 | |
895 | my $guard = AnyEvent::post_detect { |
973 | my $guard = AnyEvent::post_detect { |
896 | $WATCHER = AnyEvent->io (fh => IO::AIO::poll_fileno, poll => 'r', cb => \&IO::AIO::poll_cb); |
974 | $WATCHER = AnyEvent->io (fh => IO::AIO::poll_fileno, poll => 'r', cb => \&IO::AIO::poll_cb); |
… | |
… | |
904 | $WATCHER ||= $guard; |
982 | $WATCHER ||= $guard; |
905 | |
983 | |
906 | =item @AnyEvent::post_detect |
984 | =item @AnyEvent::post_detect |
907 | |
985 | |
908 | If there are any code references in this array (you can C<push> to it |
986 | If there are any code references in this array (you can C<push> to it |
909 | before or after loading AnyEvent), then they will called directly after |
987 | before or after loading AnyEvent), then they will be called directly |
910 | the event loop has been chosen. |
988 | after the event loop has been chosen. |
911 | |
989 | |
912 | You should check C<$AnyEvent::MODEL> before adding to this array, though: |
990 | You should check C<$AnyEvent::MODEL> before adding to this array, though: |
913 | if it is defined then the event loop has already been detected, and the |
991 | if it is defined then the event loop has already been detected, and the |
914 | array will be ignored. |
992 | array will be ignored. |
915 | |
993 | |
916 | Best use C<AnyEvent::post_detect { BLOCK }> when your application allows |
994 | Best use C<AnyEvent::post_detect { BLOCK }> when your application allows |
917 | it,as it takes care of these details. |
995 | it, as it takes care of these details. |
918 | |
996 | |
919 | This variable is mainly useful for modules that can do something useful |
997 | This variable is mainly useful for modules that can do something useful |
920 | when AnyEvent is used and thus want to know when it is initialised, but do |
998 | when AnyEvent is used and thus want to know when it is initialised, but do |
921 | not need to even load it by default. This array provides the means to hook |
999 | not need to even load it by default. This array provides the means to hook |
922 | into AnyEvent passively, without loading it. |
1000 | into AnyEvent passively, without loading it. |
923 | |
1001 | |
|
|
1002 | Example: To load Coro::AnyEvent whenever Coro and AnyEvent are used |
|
|
1003 | together, you could put this into Coro (this is the actual code used by |
|
|
1004 | Coro to accomplish this): |
|
|
1005 | |
|
|
1006 | if (defined $AnyEvent::MODEL) { |
|
|
1007 | # AnyEvent already initialised, so load Coro::AnyEvent |
|
|
1008 | require Coro::AnyEvent; |
|
|
1009 | } else { |
|
|
1010 | # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent |
|
|
1011 | # as soon as it is |
|
|
1012 | push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; |
|
|
1013 | } |
|
|
1014 | |
924 | =back |
1015 | =back |
925 | |
1016 | |
926 | =head1 WHAT TO DO IN A MODULE |
1017 | =head1 WHAT TO DO IN A MODULE |
927 | |
1018 | |
928 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
1019 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
… | |
… | |
938 | because it will stall the whole program, and the whole point of using |
1029 | because it will stall the whole program, and the whole point of using |
939 | events is to stay interactive. |
1030 | events is to stay interactive. |
940 | |
1031 | |
941 | It is fine, however, to call C<< ->recv >> when the user of your module |
1032 | It is fine, however, to call C<< ->recv >> when the user of your module |
942 | requests it (i.e. if you create a http request object ad have a method |
1033 | requests it (i.e. if you create a http request object ad have a method |
943 | called C<results> that returns the results, it should call C<< ->recv >> |
1034 | called C<results> that returns the results, it may call C<< ->recv >> |
944 | freely, as the user of your module knows what she is doing. always). |
1035 | freely, as the user of your module knows what she is doing. Always). |
945 | |
1036 | |
946 | =head1 WHAT TO DO IN THE MAIN PROGRAM |
1037 | =head1 WHAT TO DO IN THE MAIN PROGRAM |
947 | |
1038 | |
948 | There will always be a single main program - the only place that should |
1039 | There will always be a single main program - the only place that should |
949 | dictate which event model to use. |
1040 | dictate which event model to use. |
950 | |
1041 | |
951 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
1042 | If the program is not event-based, it need not do anything special, even |
952 | do anything special (it does not need to be event-based) and let AnyEvent |
1043 | when it depends on a module that uses an AnyEvent. If the program itself |
953 | decide which implementation to chose if some module relies on it. |
1044 | uses AnyEvent, but does not care which event loop is used, all it needs |
|
|
1045 | to do is C<use AnyEvent>. In either case, AnyEvent will choose the best |
|
|
1046 | available loop implementation. |
954 | |
1047 | |
955 | If the main program relies on a specific event model - for example, in |
1048 | If the main program relies on a specific event model - for example, in |
956 | Gtk2 programs you have to rely on the Glib module - you should load the |
1049 | Gtk2 programs you have to rely on the Glib module - you should load the |
957 | event module before loading AnyEvent or any module that uses it: generally |
1050 | event module before loading AnyEvent or any module that uses it: generally |
958 | speaking, you should load it as early as possible. The reason is that |
1051 | speaking, you should load it as early as possible. The reason is that |
959 | modules might create watchers when they are loaded, and AnyEvent will |
1052 | modules might create watchers when they are loaded, and AnyEvent will |
960 | decide on the event model to use as soon as it creates watchers, and it |
1053 | decide on the event model to use as soon as it creates watchers, and it |
961 | might chose the wrong one unless you load the correct one yourself. |
1054 | might choose the wrong one unless you load the correct one yourself. |
962 | |
1055 | |
963 | You can chose to use a pure-perl implementation by loading the |
1056 | You can chose to use a pure-perl implementation by loading the |
964 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
1057 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
965 | everywhere, but letting AnyEvent chose the model is generally better. |
1058 | everywhere, but letting AnyEvent chose the model is generally better. |
966 | |
1059 | |
… | |
… | |
984 | =head1 OTHER MODULES |
1077 | =head1 OTHER MODULES |
985 | |
1078 | |
986 | The following is a non-exhaustive list of additional modules that use |
1079 | The following is a non-exhaustive list of additional modules that use |
987 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
1080 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
988 | modules and other event loops in the same program. Some of the modules |
1081 | modules and other event loops in the same program. Some of the modules |
989 | come with AnyEvent, most are available via CPAN. |
1082 | come as part of AnyEvent, the others are available via CPAN. |
990 | |
1083 | |
991 | =over 4 |
1084 | =over 4 |
992 | |
1085 | |
993 | =item L<AnyEvent::Util> |
1086 | =item L<AnyEvent::Util> |
994 | |
1087 | |
995 | Contains various utility functions that replace often-used but blocking |
1088 | Contains various utility functions that replace often-used blocking |
996 | functions such as C<inet_aton> by event-/callback-based versions. |
1089 | functions such as C<inet_aton> with event/callback-based versions. |
997 | |
1090 | |
998 | =item L<AnyEvent::Socket> |
1091 | =item L<AnyEvent::Socket> |
999 | |
1092 | |
1000 | Provides various utility functions for (internet protocol) sockets, |
1093 | Provides various utility functions for (internet protocol) sockets, |
1001 | addresses and name resolution. Also functions to create non-blocking tcp |
1094 | addresses and name resolution. Also functions to create non-blocking tcp |
… | |
… | |
1003 | |
1096 | |
1004 | =item L<AnyEvent::Handle> |
1097 | =item L<AnyEvent::Handle> |
1005 | |
1098 | |
1006 | Provide read and write buffers, manages watchers for reads and writes, |
1099 | Provide read and write buffers, manages watchers for reads and writes, |
1007 | supports raw and formatted I/O, I/O queued and fully transparent and |
1100 | supports raw and formatted I/O, I/O queued and fully transparent and |
1008 | non-blocking SSL/TLS (via L<AnyEvent::TLS>. |
1101 | non-blocking SSL/TLS (via L<AnyEvent::TLS>). |
1009 | |
1102 | |
1010 | =item L<AnyEvent::DNS> |
1103 | =item L<AnyEvent::DNS> |
1011 | |
1104 | |
1012 | Provides rich asynchronous DNS resolver capabilities. |
1105 | Provides rich asynchronous DNS resolver capabilities. |
1013 | |
1106 | |
|
|
1107 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
|
|
1108 | |
|
|
1109 | Implement event-based interfaces to the protocols of the same name (for |
|
|
1110 | the curious, IGS is the International Go Server and FCP is the Freenet |
|
|
1111 | Client Protocol). |
|
|
1112 | |
|
|
1113 | =item L<AnyEvent::Handle::UDP> |
|
|
1114 | |
|
|
1115 | Here be danger! |
|
|
1116 | |
|
|
1117 | As Pauli would put it, "Not only is it not right, it's not even wrong!" - |
|
|
1118 | there are so many things wrong with AnyEvent::Handle::UDP, most notably |
|
|
1119 | its use of a stream-based API with a protocol that isn't streamable, that |
|
|
1120 | the only way to improve it is to delete it. |
|
|
1121 | |
|
|
1122 | It features data corruption (but typically only under load) and general |
|
|
1123 | confusion. On top, the author is not only clueless about UDP but also |
|
|
1124 | fact-resistant - some gems of his understanding: "connect doesn't work |
|
|
1125 | with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not |
|
|
1126 | packets", "I don't need to implement proper error checking as UDP doesn't |
|
|
1127 | support error checking" and so on - he doesn't even understand what's |
|
|
1128 | wrong with his module when it is explained to him. |
|
|
1129 | |
1014 | =item L<AnyEvent::HTTP> |
1130 | =item L<AnyEvent::DBI> |
1015 | |
1131 | |
1016 | A simple-to-use HTTP library that is capable of making a lot of concurrent |
1132 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1017 | HTTP requests. |
1133 | notifying you in an event-based way when the operation is finished. |
|
|
1134 | |
|
|
1135 | =item L<AnyEvent::AIO> |
|
|
1136 | |
|
|
1137 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
|
|
1138 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
|
|
1139 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
|
|
1140 | file I/O, and much more. |
1018 | |
1141 | |
1019 | =item L<AnyEvent::HTTPD> |
1142 | =item L<AnyEvent::HTTPD> |
1020 | |
1143 | |
1021 | Provides a simple web application server framework. |
1144 | A simple embedded webserver. |
1022 | |
1145 | |
1023 | =item L<AnyEvent::FastPing> |
1146 | =item L<AnyEvent::FastPing> |
1024 | |
1147 | |
1025 | The fastest ping in the west. |
1148 | The fastest ping in the west. |
1026 | |
|
|
1027 | =item L<AnyEvent::DBI> |
|
|
1028 | |
|
|
1029 | Executes L<DBI> requests asynchronously in a proxy process. |
|
|
1030 | |
|
|
1031 | =item L<AnyEvent::AIO> |
|
|
1032 | |
|
|
1033 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
1034 | programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent |
|
|
1035 | together. |
|
|
1036 | |
|
|
1037 | =item L<AnyEvent::BDB> |
|
|
1038 | |
|
|
1039 | Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses |
|
|
1040 | L<BDB> and AnyEvent together. |
|
|
1041 | |
|
|
1042 | =item L<AnyEvent::GPSD> |
|
|
1043 | |
|
|
1044 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
|
|
1045 | |
|
|
1046 | =item L<AnyEvent::IRC> |
|
|
1047 | |
|
|
1048 | AnyEvent based IRC client module family (replacing the older Net::IRC3). |
|
|
1049 | |
|
|
1050 | =item L<AnyEvent::XMPP> |
|
|
1051 | |
|
|
1052 | AnyEvent based XMPP (Jabber protocol) module family (replacing the older |
|
|
1053 | Net::XMPP2>. |
|
|
1054 | |
|
|
1055 | =item L<AnyEvent::IGS> |
|
|
1056 | |
|
|
1057 | A non-blocking interface to the Internet Go Server protocol (used by |
|
|
1058 | L<App::IGS>). |
|
|
1059 | |
|
|
1060 | =item L<Net::FCP> |
|
|
1061 | |
|
|
1062 | AnyEvent-based implementation of the Freenet Client Protocol, birthplace |
|
|
1063 | of AnyEvent. |
|
|
1064 | |
|
|
1065 | =item L<Event::ExecFlow> |
|
|
1066 | |
|
|
1067 | High level API for event-based execution flow control. |
|
|
1068 | |
1149 | |
1069 | =item L<Coro> |
1150 | =item L<Coro> |
1070 | |
1151 | |
1071 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
1152 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
1072 | |
1153 | |
… | |
… | |
1076 | |
1157 | |
1077 | package AnyEvent; |
1158 | package AnyEvent; |
1078 | |
1159 | |
1079 | # basically a tuned-down version of common::sense |
1160 | # basically a tuned-down version of common::sense |
1080 | sub common_sense { |
1161 | sub common_sense { |
1081 | # no warnings |
1162 | # from common:.sense 3.3 |
1082 | ${^WARNING_BITS} ^= ${^WARNING_BITS}; |
1163 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf3\x0f\xc0\xf0\xfc\x33\x00"; |
1083 | # use strict vars subs |
1164 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
1084 | $^H |= 0x00000600; |
1165 | $^H |= 0x00000600; |
1085 | } |
1166 | } |
1086 | |
1167 | |
1087 | BEGIN { AnyEvent::common_sense } |
1168 | BEGIN { AnyEvent::common_sense } |
1088 | |
1169 | |
1089 | use Carp (); |
1170 | use Carp (); |
1090 | |
1171 | |
1091 | our $VERSION = 4.881; |
1172 | our $VERSION = '5.29'; |
1092 | our $MODEL; |
1173 | our $MODEL; |
1093 | |
1174 | |
1094 | our $AUTOLOAD; |
1175 | our $AUTOLOAD; |
1095 | our @ISA; |
1176 | our @ISA; |
1096 | |
1177 | |
1097 | our @REGISTRY; |
1178 | our @REGISTRY; |
1098 | |
1179 | |
1099 | our $WIN32; |
|
|
1100 | |
|
|
1101 | our $VERBOSE; |
1180 | our $VERBOSE; |
1102 | |
1181 | |
1103 | BEGIN { |
1182 | BEGIN { |
1104 | eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; |
1183 | require "AnyEvent/constants.pl"; |
|
|
1184 | |
1105 | eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; |
1185 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1106 | |
1186 | |
1107 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1187 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1108 | if ${^TAINT}; |
1188 | if ${^TAINT}; |
1109 | |
1189 | |
1110 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1190 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
… | |
… | |
1122 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1202 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1123 | } |
1203 | } |
1124 | |
1204 | |
1125 | my @models = ( |
1205 | my @models = ( |
1126 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1206 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1127 | [Event:: => AnyEvent::Impl::Event::, 1], |
|
|
1128 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl:: , 1], |
1207 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl:: , 1], |
1129 | # everything below here will not (normally) be autoprobed |
1208 | # everything below here will not (normally) be autoprobed |
1130 | # as the pureperl backend should work everywhere |
1209 | # as the pureperl backend should work everywhere |
1131 | # and is usually faster |
1210 | # and is usually faster |
|
|
1211 | [Event:: => AnyEvent::Impl::Event::, 1], |
1132 | [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers |
1212 | [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers |
1133 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1213 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1134 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
1214 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
1135 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1215 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1136 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1216 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
… | |
… | |
1139 | [Prima:: => AnyEvent::Impl::POE::], |
1219 | [Prima:: => AnyEvent::Impl::POE::], |
1140 | # IO::Async is just too broken - we would need workarounds for its |
1220 | # IO::Async is just too broken - we would need workarounds for its |
1141 | # byzantine signal and broken child handling, among others. |
1221 | # byzantine signal and broken child handling, among others. |
1142 | # IO::Async is rather hard to detect, as it doesn't have any |
1222 | # IO::Async is rather hard to detect, as it doesn't have any |
1143 | # obvious default class. |
1223 | # obvious default class. |
1144 | # [0, IO::Async:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1224 | [IO::Async:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1145 | # [0, IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1225 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1146 | # [0, IO::Async::Notifier:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1226 | [IO::Async::Notifier:: => AnyEvent::Impl::IOAsync::], # requires special main program |
|
|
1227 | [AnyEvent::Impl::IOAsync:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1147 | ); |
1228 | ); |
1148 | |
1229 | |
1149 | our %method = map +($_ => 1), |
1230 | our %method = map +($_ => 1), |
1150 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
1231 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
1151 | |
1232 | |
1152 | our @post_detect; |
1233 | our @post_detect; |
1153 | |
1234 | |
1154 | sub post_detect(&) { |
1235 | sub post_detect(&) { |
1155 | my ($cb) = @_; |
1236 | my ($cb) = @_; |
1156 | |
1237 | |
1157 | if ($MODEL) { |
|
|
1158 | $cb->(); |
|
|
1159 | |
|
|
1160 | undef |
|
|
1161 | } else { |
|
|
1162 | push @post_detect, $cb; |
1238 | push @post_detect, $cb; |
1163 | |
1239 | |
1164 | defined wantarray |
1240 | defined wantarray |
1165 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1241 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1166 | : () |
1242 | : () |
1167 | } |
|
|
1168 | } |
1243 | } |
1169 | |
1244 | |
1170 | sub AnyEvent::Util::postdetect::DESTROY { |
1245 | sub AnyEvent::Util::postdetect::DESTROY { |
1171 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1246 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1172 | } |
1247 | } |
1173 | |
1248 | |
1174 | sub detect() { |
1249 | sub detect() { |
|
|
1250 | # free some memory |
|
|
1251 | *detect = sub () { $MODEL }; |
|
|
1252 | |
|
|
1253 | local $!; # for good measure |
|
|
1254 | local $SIG{__DIE__}; |
|
|
1255 | |
|
|
1256 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
|
|
1257 | my $model = "AnyEvent::Impl::$1"; |
|
|
1258 | if (eval "require $model") { |
|
|
1259 | $MODEL = $model; |
|
|
1260 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
|
|
1261 | } else { |
|
|
1262 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
|
|
1263 | } |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | # check for already loaded models |
1175 | unless ($MODEL) { |
1267 | unless ($MODEL) { |
1176 | local $SIG{__DIE__}; |
1268 | for (@REGISTRY, @models) { |
1177 | |
1269 | my ($package, $model) = @$_; |
1178 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
1270 | if (${"$package\::VERSION"} > 0) { |
1179 | my $model = "AnyEvent::Impl::$1"; |
|
|
1180 | if (eval "require $model") { |
1271 | if (eval "require $model") { |
1181 | $MODEL = $model; |
1272 | $MODEL = $model; |
1182 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
1273 | warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; |
1183 | } else { |
1274 | last; |
1184 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
1275 | } |
1185 | } |
1276 | } |
1186 | } |
1277 | } |
1187 | |
1278 | |
1188 | # check for already loaded models |
|
|
1189 | unless ($MODEL) { |
1279 | unless ($MODEL) { |
|
|
1280 | # try to autoload a model |
1190 | for (@REGISTRY, @models) { |
1281 | for (@REGISTRY, @models) { |
1191 | my ($package, $model) = @$_; |
1282 | my ($package, $model, $autoload) = @$_; |
|
|
1283 | if ( |
|
|
1284 | $autoload |
|
|
1285 | and eval "require $package" |
1192 | if (${"$package\::VERSION"} > 0) { |
1286 | and ${"$package\::VERSION"} > 0 |
1193 | if (eval "require $model") { |
1287 | and eval "require $model" |
|
|
1288 | ) { |
1194 | $MODEL = $model; |
1289 | $MODEL = $model; |
1195 | warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; |
1290 | warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; |
1196 | last; |
1291 | last; |
1197 | } |
|
|
1198 | } |
1292 | } |
1199 | } |
1293 | } |
1200 | |
1294 | |
1201 | unless ($MODEL) { |
|
|
1202 | # try to autoload a model |
|
|
1203 | for (@REGISTRY, @models) { |
|
|
1204 | my ($package, $model, $autoload) = @$_; |
|
|
1205 | if ( |
|
|
1206 | $autoload |
|
|
1207 | and eval "require $package" |
|
|
1208 | and ${"$package\::VERSION"} > 0 |
|
|
1209 | and eval "require $model" |
|
|
1210 | ) { |
|
|
1211 | $MODEL = $model; |
|
|
1212 | warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; |
|
|
1213 | last; |
|
|
1214 | } |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | $MODEL |
1295 | $MODEL |
1218 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; |
1296 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1219 | } |
|
|
1220 | } |
1297 | } |
1221 | |
|
|
1222 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
|
|
1223 | |
|
|
1224 | unshift @ISA, $MODEL; |
|
|
1225 | |
|
|
1226 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
|
|
1227 | |
|
|
1228 | (shift @post_detect)->() while @post_detect; |
|
|
1229 | } |
1298 | } |
|
|
1299 | |
|
|
1300 | @models = (); # free probe data |
|
|
1301 | |
|
|
1302 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
|
|
1303 | unshift @ISA, $MODEL; |
|
|
1304 | |
|
|
1305 | # now nuke some methods that are overridden by the backend. |
|
|
1306 | # SUPER is not allowed. |
|
|
1307 | for (qw(time signal child idle)) { |
|
|
1308 | undef &{"AnyEvent::Base::$_"} |
|
|
1309 | if defined &{"$MODEL\::$_"}; |
|
|
1310 | } |
|
|
1311 | |
|
|
1312 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
|
|
1313 | |
|
|
1314 | (shift @post_detect)->() while @post_detect; |
|
|
1315 | |
|
|
1316 | *post_detect = sub(&) { |
|
|
1317 | shift->(); |
|
|
1318 | |
|
|
1319 | undef |
|
|
1320 | }; |
1230 | |
1321 | |
1231 | $MODEL |
1322 | $MODEL |
1232 | } |
1323 | } |
1233 | |
1324 | |
1234 | sub AUTOLOAD { |
1325 | sub AUTOLOAD { |
1235 | (my $func = $AUTOLOAD) =~ s/.*://; |
1326 | (my $func = $AUTOLOAD) =~ s/.*://; |
1236 | |
1327 | |
1237 | $method{$func} |
1328 | $method{$func} |
1238 | or Carp::croak "$func: not a valid method for AnyEvent objects"; |
1329 | or Carp::croak "$func: not a valid AnyEvent class method"; |
1239 | |
1330 | |
1240 | detect unless $MODEL; |
1331 | detect; |
1241 | |
1332 | |
1242 | my $class = shift; |
1333 | my $class = shift; |
1243 | $class->$func (@_); |
1334 | $class->$func (@_); |
1244 | } |
1335 | } |
1245 | |
1336 | |
… | |
… | |
1258 | # we assume CLOEXEC is already set by perl in all important cases |
1349 | # we assume CLOEXEC is already set by perl in all important cases |
1259 | |
1350 | |
1260 | ($fh2, $rw) |
1351 | ($fh2, $rw) |
1261 | } |
1352 | } |
1262 | |
1353 | |
|
|
1354 | =head1 SIMPLIFIED AE API |
|
|
1355 | |
|
|
1356 | Starting with version 5.0, AnyEvent officially supports a second, much |
|
|
1357 | simpler, API that is designed to reduce the calling, typing and memory |
|
|
1358 | overhead by using function call syntax and a fixed number of parameters. |
|
|
1359 | |
|
|
1360 | See the L<AE> manpage for details. |
|
|
1361 | |
|
|
1362 | =cut |
|
|
1363 | |
|
|
1364 | package AE; |
|
|
1365 | |
|
|
1366 | our $VERSION = $AnyEvent::VERSION; |
|
|
1367 | |
|
|
1368 | # fall back to the main API by default - backends and AnyEvent::Base |
|
|
1369 | # implementations can overwrite these. |
|
|
1370 | |
|
|
1371 | sub io($$$) { |
|
|
1372 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
|
|
1373 | } |
|
|
1374 | |
|
|
1375 | sub timer($$$) { |
|
|
1376 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) |
|
|
1377 | } |
|
|
1378 | |
|
|
1379 | sub signal($$) { |
|
|
1380 | AnyEvent->signal (signal => $_[0], cb => $_[1]) |
|
|
1381 | } |
|
|
1382 | |
|
|
1383 | sub child($$) { |
|
|
1384 | AnyEvent->child (pid => $_[0], cb => $_[1]) |
|
|
1385 | } |
|
|
1386 | |
|
|
1387 | sub idle($) { |
|
|
1388 | AnyEvent->idle (cb => $_[0]) |
|
|
1389 | } |
|
|
1390 | |
|
|
1391 | sub cv(;&) { |
|
|
1392 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
|
|
1393 | } |
|
|
1394 | |
|
|
1395 | sub now() { |
|
|
1396 | AnyEvent->now |
|
|
1397 | } |
|
|
1398 | |
|
|
1399 | sub now_update() { |
|
|
1400 | AnyEvent->now_update |
|
|
1401 | } |
|
|
1402 | |
|
|
1403 | sub time() { |
|
|
1404 | AnyEvent->time |
|
|
1405 | } |
|
|
1406 | |
1263 | package AnyEvent::Base; |
1407 | package AnyEvent::Base; |
1264 | |
1408 | |
1265 | # default implementations for many methods |
1409 | # default implementations for many methods |
1266 | |
1410 | |
1267 | sub _time { |
1411 | sub time { |
|
|
1412 | eval q{ # poor man's autoloading {} |
1268 | # probe for availability of Time::HiRes |
1413 | # probe for availability of Time::HiRes |
1269 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1414 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1270 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1415 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1271 | *_time = \&Time::HiRes::time; |
1416 | *AE::time = \&Time::HiRes::time; |
1272 | # if (eval "use POSIX (); (POSIX::times())... |
1417 | # if (eval "use POSIX (); (POSIX::times())... |
1273 | } else { |
1418 | } else { |
1274 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1419 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1275 | *_time = sub { time }; # epic fail |
1420 | *AE::time = sub (){ time }; # epic fail |
|
|
1421 | } |
|
|
1422 | |
|
|
1423 | *time = sub { AE::time }; # different prototypes |
1276 | } |
1424 | }; |
|
|
1425 | die if $@; |
1277 | |
1426 | |
1278 | &_time |
1427 | &time |
1279 | } |
1428 | } |
1280 | |
1429 | |
1281 | sub time { _time } |
1430 | *now = \&time; |
1282 | sub now { _time } |
1431 | |
1283 | sub now_update { } |
1432 | sub now_update { } |
1284 | |
1433 | |
1285 | # default implementation for ->condvar |
1434 | # default implementation for ->condvar |
1286 | |
1435 | |
1287 | sub condvar { |
1436 | sub condvar { |
|
|
1437 | eval q{ # poor man's autoloading {} |
|
|
1438 | *condvar = sub { |
1288 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1439 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
|
|
1440 | }; |
|
|
1441 | |
|
|
1442 | *AE::cv = sub (;&) { |
|
|
1443 | bless { @_ ? (_ae_cb => shift) : () }, "AnyEvent::CondVar" |
|
|
1444 | }; |
|
|
1445 | }; |
|
|
1446 | die if $@; |
|
|
1447 | |
|
|
1448 | &condvar |
1289 | } |
1449 | } |
1290 | |
1450 | |
1291 | # default implementation for ->signal |
1451 | # default implementation for ->signal |
1292 | |
1452 | |
1293 | our $HAVE_ASYNC_INTERRUPT; |
1453 | our $HAVE_ASYNC_INTERRUPT; |
1294 | |
1454 | |
1295 | sub _have_async_interrupt() { |
1455 | sub _have_async_interrupt() { |
1296 | $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} |
1456 | $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} |
1297 | && eval "use Async::Interrupt 1.0 (); 1") |
1457 | && eval "use Async::Interrupt 1.02 (); 1") |
1298 | unless defined $HAVE_ASYNC_INTERRUPT; |
1458 | unless defined $HAVE_ASYNC_INTERRUPT; |
1299 | |
1459 | |
1300 | $HAVE_ASYNC_INTERRUPT |
1460 | $HAVE_ASYNC_INTERRUPT |
1301 | } |
1461 | } |
1302 | |
1462 | |
1303 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
1463 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
1304 | our (%SIG_ASY, %SIG_ASY_W); |
1464 | our (%SIG_ASY, %SIG_ASY_W); |
1305 | our ($SIG_COUNT, $SIG_TW); |
1465 | our ($SIG_COUNT, $SIG_TW); |
1306 | |
1466 | |
1307 | sub _signal_exec { |
|
|
1308 | $HAVE_ASYNC_INTERRUPT |
|
|
1309 | ? $SIGPIPE_R->drain |
|
|
1310 | : sysread $SIGPIPE_R, my $dummy, 9; |
|
|
1311 | |
|
|
1312 | while (%SIG_EV) { |
|
|
1313 | for (keys %SIG_EV) { |
|
|
1314 | delete $SIG_EV{$_}; |
|
|
1315 | $_->() for values %{ $SIG_CB{$_} || {} }; |
|
|
1316 | } |
|
|
1317 | } |
|
|
1318 | } |
|
|
1319 | |
|
|
1320 | # install a dummy wakeup watcher to reduce signal catching latency |
1467 | # install a dummy wakeup watcher to reduce signal catching latency |
|
|
1468 | # used by Impls |
1321 | sub _sig_add() { |
1469 | sub _sig_add() { |
1322 | unless ($SIG_COUNT++) { |
1470 | unless ($SIG_COUNT++) { |
1323 | # try to align timer on a full-second boundary, if possible |
1471 | # try to align timer on a full-second boundary, if possible |
1324 | my $NOW = AnyEvent->now; |
1472 | my $NOW = AE::now; |
1325 | |
1473 | |
1326 | $SIG_TW = AnyEvent->timer ( |
1474 | $SIG_TW = AE::timer |
1327 | after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
1475 | $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
1328 | interval => $MAX_SIGNAL_LATENCY, |
1476 | $MAX_SIGNAL_LATENCY, |
1329 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
1477 | sub { } # just for the PERL_ASYNC_CHECK |
1330 | ); |
1478 | ; |
1331 | } |
1479 | } |
1332 | } |
1480 | } |
1333 | |
1481 | |
1334 | sub _sig_del { |
1482 | sub _sig_del { |
1335 | undef $SIG_TW |
1483 | undef $SIG_TW |
1336 | unless --$SIG_COUNT; |
1484 | unless --$SIG_COUNT; |
1337 | } |
1485 | } |
1338 | |
1486 | |
1339 | our $_sig_name_init; $_sig_name_init = sub { |
1487 | our $_sig_name_init; $_sig_name_init = sub { |
|
|
1488 | eval q{ # poor man's autoloading {} |
1340 | undef $_sig_name_init; |
1489 | undef $_sig_name_init; |
1341 | |
1490 | |
1342 | if (_have_async_interrupt) { |
1491 | if (_have_async_interrupt) { |
1343 | *sig2num = \&Async::Interrupt::sig2num; |
1492 | *sig2num = \&Async::Interrupt::sig2num; |
1344 | *sig2name = \&Async::Interrupt::sig2name; |
1493 | *sig2name = \&Async::Interrupt::sig2name; |
1345 | } else { |
1494 | } else { |
1346 | require Config; |
1495 | require Config; |
1347 | |
1496 | |
1348 | my %signame2num; |
1497 | my %signame2num; |
1349 | @signame2num{ split ' ', $Config::Config{sig_name} } |
1498 | @signame2num{ split ' ', $Config::Config{sig_name} } |
1350 | = split ' ', $Config::Config{sig_num}; |
1499 | = split ' ', $Config::Config{sig_num}; |
1351 | |
1500 | |
1352 | my @signum2name; |
1501 | my @signum2name; |
1353 | @signum2name[values %signame2num] = keys %signame2num; |
1502 | @signum2name[values %signame2num] = keys %signame2num; |
1354 | |
1503 | |
1355 | *sig2num = sub($) { |
1504 | *sig2num = sub($) { |
1356 | $_[0] > 0 ? shift : $signame2num{+shift} |
1505 | $_[0] > 0 ? shift : $signame2num{+shift} |
1357 | }; |
1506 | }; |
1358 | *sig2name = sub ($) { |
1507 | *sig2name = sub ($) { |
1359 | $_[0] > 0 ? $signum2name[+shift] : shift |
1508 | $_[0] > 0 ? $signum2name[+shift] : shift |
|
|
1509 | }; |
1360 | }; |
1510 | } |
1361 | } |
1511 | }; |
|
|
1512 | die if $@; |
1362 | }; |
1513 | }; |
1363 | |
1514 | |
1364 | sub sig2num ($) { &$_sig_name_init; &sig2num } |
1515 | sub sig2num ($) { &$_sig_name_init; &sig2num } |
1365 | sub sig2name($) { &$_sig_name_init; &sig2name } |
1516 | sub sig2name($) { &$_sig_name_init; &sig2name } |
1366 | |
1517 | |
1367 | sub _signal { |
1518 | sub signal { |
|
|
1519 | eval q{ # poor man's autoloading {} |
|
|
1520 | # probe for availability of Async::Interrupt |
|
|
1521 | if (_have_async_interrupt) { |
|
|
1522 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
|
|
1523 | |
|
|
1524 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
|
|
1525 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
|
|
1526 | |
|
|
1527 | } else { |
|
|
1528 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
|
|
1529 | |
|
|
1530 | if (AnyEvent::WIN32) { |
|
|
1531 | require AnyEvent::Util; |
|
|
1532 | |
|
|
1533 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
|
|
1534 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; |
|
|
1535 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case |
|
|
1536 | } else { |
|
|
1537 | pipe $SIGPIPE_R, $SIGPIPE_W; |
|
|
1538 | fcntl $SIGPIPE_R, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_R; |
|
|
1539 | fcntl $SIGPIPE_W, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_W; # just in case |
|
|
1540 | |
|
|
1541 | # not strictly required, as $^F is normally 2, but let's make sure... |
|
|
1542 | fcntl $SIGPIPE_R, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
|
|
1543 | fcntl $SIGPIPE_W, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
|
|
1544 | } |
|
|
1545 | |
|
|
1546 | $SIGPIPE_R |
|
|
1547 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
|
|
1548 | |
|
|
1549 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
|
|
1550 | } |
|
|
1551 | |
|
|
1552 | *signal = $HAVE_ASYNC_INTERRUPT |
|
|
1553 | ? sub { |
1368 | my (undef, %arg) = @_; |
1554 | my (undef, %arg) = @_; |
1369 | |
1555 | |
1370 | my $signal = uc $arg{signal} |
|
|
1371 | or Carp::croak "required option 'signal' is missing"; |
|
|
1372 | |
|
|
1373 | if ($HAVE_ASYNC_INTERRUPT) { |
|
|
1374 | # async::interrupt |
1556 | # async::interrupt |
1375 | |
|
|
1376 | $signal = sig2num $signal; |
1557 | my $signal = sig2num $arg{signal}; |
1377 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1558 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1378 | |
1559 | |
1379 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1560 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1380 | cb => sub { undef $SIG_EV{$signal} }, |
1561 | cb => sub { undef $SIG_EV{$signal} }, |
1381 | signal => $signal, |
1562 | signal => $signal, |
1382 | pipe => [$SIGPIPE_R->filenos], |
1563 | pipe => [$SIGPIPE_R->filenos], |
1383 | pipe_autodrain => 0, |
1564 | pipe_autodrain => 0, |
|
|
1565 | ; |
|
|
1566 | |
|
|
1567 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1568 | } |
|
|
1569 | : sub { |
|
|
1570 | my (undef, %arg) = @_; |
|
|
1571 | |
|
|
1572 | # pure perl |
|
|
1573 | my $signal = sig2name $arg{signal}; |
|
|
1574 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
|
|
1575 | |
|
|
1576 | $SIG{$signal} ||= sub { |
|
|
1577 | local $!; |
|
|
1578 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
|
|
1579 | undef $SIG_EV{$signal}; |
|
|
1580 | }; |
|
|
1581 | |
|
|
1582 | # can't do signal processing without introducing races in pure perl, |
|
|
1583 | # so limit the signal latency. |
|
|
1584 | _sig_add; |
|
|
1585 | |
|
|
1586 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1587 | } |
1384 | ; |
1588 | ; |
1385 | |
1589 | |
1386 | } else { |
1590 | *AnyEvent::Base::signal::DESTROY = sub { |
1387 | # pure perl |
1591 | my ($signal, $cb) = @{$_[0]}; |
1388 | |
1592 | |
1389 | # AE::Util has been loaded in signal |
1593 | _sig_del; |
1390 | $signal = sig2name $signal; |
|
|
1391 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
|
|
1392 | |
1594 | |
1393 | $SIG{$signal} ||= sub { |
1595 | delete $SIG_CB{$signal}{$cb}; |
1394 | local $!; |
1596 | |
1395 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1597 | $HAVE_ASYNC_INTERRUPT |
|
|
1598 | ? delete $SIG_ASY{$signal} |
|
|
1599 | : # delete doesn't work with older perls - they then |
|
|
1600 | # print weird messages, or just unconditionally exit |
|
|
1601 | # instead of getting the default action. |
1396 | undef $SIG_EV{$signal}; |
1602 | undef $SIG{$signal} |
|
|
1603 | unless keys %{ $SIG_CB{$signal} }; |
1397 | }; |
1604 | }; |
1398 | |
1605 | |
1399 | # can't do signal processing without introducing races in pure perl, |
1606 | *_signal_exec = sub { |
1400 | # so limit the signal latency. |
1607 | $HAVE_ASYNC_INTERRUPT |
1401 | _sig_add; |
1608 | ? $SIGPIPE_R->drain |
1402 | } |
1609 | : sysread $SIGPIPE_R, (my $dummy), 9; |
1403 | |
1610 | |
1404 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1611 | while (%SIG_EV) { |
1405 | } |
1612 | for (keys %SIG_EV) { |
1406 | |
1613 | delete $SIG_EV{$_}; |
1407 | sub signal { |
1614 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1408 | # probe for availability of Async::Interrupt |
1615 | } |
1409 | if (_have_async_interrupt) { |
1616 | } |
1410 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
|
|
1411 | |
|
|
1412 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
|
|
1413 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); |
|
|
1414 | |
|
|
1415 | } else { |
|
|
1416 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
|
|
1417 | |
|
|
1418 | require Fcntl; |
|
|
1419 | |
|
|
1420 | if (AnyEvent::WIN32) { |
|
|
1421 | require AnyEvent::Util; |
|
|
1422 | |
|
|
1423 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
|
|
1424 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R; |
|
|
1425 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case |
|
|
1426 | } else { |
|
|
1427 | pipe $SIGPIPE_R, $SIGPIPE_W; |
|
|
1428 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
|
|
1429 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
|
|
1430 | |
|
|
1431 | # not strictly required, as $^F is normally 2, but let's make sure... |
|
|
1432 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1433 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1434 | } |
1617 | }; |
1435 | |
|
|
1436 | $SIGPIPE_R |
|
|
1437 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
|
|
1438 | |
|
|
1439 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
|
|
1440 | } |
1618 | }; |
|
|
1619 | die if $@; |
1441 | |
1620 | |
1442 | *signal = \&_signal; |
|
|
1443 | &signal |
1621 | &signal |
1444 | } |
|
|
1445 | |
|
|
1446 | sub AnyEvent::Base::signal::DESTROY { |
|
|
1447 | my ($signal, $cb) = @{$_[0]}; |
|
|
1448 | |
|
|
1449 | _sig_del; |
|
|
1450 | |
|
|
1451 | delete $SIG_CB{$signal}{$cb}; |
|
|
1452 | |
|
|
1453 | $HAVE_ASYNC_INTERRUPT |
|
|
1454 | ? delete $SIG_ASY{$signal} |
|
|
1455 | : # delete doesn't work with older perls - they then |
|
|
1456 | # print weird messages, or just unconditionally exit |
|
|
1457 | # instead of getting the default action. |
|
|
1458 | undef $SIG{$signal} |
|
|
1459 | unless keys %{ $SIG_CB{$signal} }; |
|
|
1460 | } |
1622 | } |
1461 | |
1623 | |
1462 | # default implementation for ->child |
1624 | # default implementation for ->child |
1463 | |
1625 | |
1464 | our %PID_CB; |
1626 | our %PID_CB; |
1465 | our $CHLD_W; |
1627 | our $CHLD_W; |
1466 | our $CHLD_DELAY_W; |
1628 | our $CHLD_DELAY_W; |
1467 | our $WNOHANG; |
1629 | our $WNOHANG; |
1468 | |
1630 | |
|
|
1631 | # used by many Impl's |
1469 | sub _emit_childstatus($$) { |
1632 | sub _emit_childstatus($$) { |
1470 | my (undef, $rpid, $rstatus) = @_; |
1633 | my (undef, $rpid, $rstatus) = @_; |
1471 | |
1634 | |
1472 | $_->($rpid, $rstatus) |
1635 | $_->($rpid, $rstatus) |
1473 | for values %{ $PID_CB{$rpid} || {} }, |
1636 | for values %{ $PID_CB{$rpid} || {} }, |
1474 | values %{ $PID_CB{0} || {} }; |
1637 | values %{ $PID_CB{0} || {} }; |
1475 | } |
1638 | } |
1476 | |
1639 | |
1477 | sub _sigchld { |
|
|
1478 | my $pid; |
|
|
1479 | |
|
|
1480 | AnyEvent->_emit_childstatus ($pid, $?) |
|
|
1481 | while ($pid = waitpid -1, $WNOHANG) > 0; |
|
|
1482 | } |
|
|
1483 | |
|
|
1484 | sub child { |
1640 | sub child { |
|
|
1641 | eval q{ # poor man's autoloading {} |
|
|
1642 | *_sigchld = sub { |
|
|
1643 | my $pid; |
|
|
1644 | |
|
|
1645 | AnyEvent->_emit_childstatus ($pid, $?) |
|
|
1646 | while ($pid = waitpid -1, $WNOHANG) > 0; |
|
|
1647 | }; |
|
|
1648 | |
|
|
1649 | *child = sub { |
1485 | my (undef, %arg) = @_; |
1650 | my (undef, %arg) = @_; |
1486 | |
1651 | |
1487 | defined (my $pid = $arg{pid} + 0) |
1652 | defined (my $pid = $arg{pid} + 0) |
1488 | or Carp::croak "required option 'pid' is missing"; |
1653 | or Carp::croak "required option 'pid' is missing"; |
1489 | |
1654 | |
1490 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1655 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1491 | |
1656 | |
1492 | # WNOHANG is almost cetrainly 1 everywhere |
1657 | # WNOHANG is almost cetrainly 1 everywhere |
1493 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1658 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1494 | ? 1 |
1659 | ? 1 |
1495 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1660 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1496 | |
1661 | |
1497 | unless ($CHLD_W) { |
1662 | unless ($CHLD_W) { |
1498 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1663 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1499 | # child could be a zombie already, so make at least one round |
1664 | # child could be a zombie already, so make at least one round |
1500 | &_sigchld; |
1665 | &_sigchld; |
1501 | } |
1666 | } |
1502 | |
1667 | |
1503 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1668 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1504 | } |
1669 | }; |
1505 | |
1670 | |
1506 | sub AnyEvent::Base::child::DESTROY { |
1671 | *AnyEvent::Base::child::DESTROY = sub { |
1507 | my ($pid, $cb) = @{$_[0]}; |
1672 | my ($pid, $cb) = @{$_[0]}; |
1508 | |
1673 | |
1509 | delete $PID_CB{$pid}{$cb}; |
1674 | delete $PID_CB{$pid}{$cb}; |
1510 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1675 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1511 | |
1676 | |
1512 | undef $CHLD_W unless keys %PID_CB; |
1677 | undef $CHLD_W unless keys %PID_CB; |
|
|
1678 | }; |
|
|
1679 | }; |
|
|
1680 | die if $@; |
|
|
1681 | |
|
|
1682 | &child |
1513 | } |
1683 | } |
1514 | |
1684 | |
1515 | # idle emulation is done by simply using a timer, regardless |
1685 | # idle emulation is done by simply using a timer, regardless |
1516 | # of whether the process is idle or not, and not letting |
1686 | # of whether the process is idle or not, and not letting |
1517 | # the callback use more than 50% of the time. |
1687 | # the callback use more than 50% of the time. |
1518 | sub idle { |
1688 | sub idle { |
|
|
1689 | eval q{ # poor man's autoloading {} |
|
|
1690 | *idle = sub { |
1519 | my (undef, %arg) = @_; |
1691 | my (undef, %arg) = @_; |
1520 | |
1692 | |
1521 | my ($cb, $w, $rcb) = $arg{cb}; |
1693 | my ($cb, $w, $rcb) = $arg{cb}; |
1522 | |
1694 | |
1523 | $rcb = sub { |
1695 | $rcb = sub { |
1524 | if ($cb) { |
1696 | if ($cb) { |
1525 | $w = _time; |
1697 | $w = _time; |
1526 | &$cb; |
1698 | &$cb; |
1527 | $w = _time - $w; |
1699 | $w = _time - $w; |
1528 | |
1700 | |
1529 | # never use more then 50% of the time for the idle watcher, |
1701 | # never use more then 50% of the time for the idle watcher, |
1530 | # within some limits |
1702 | # within some limits |
1531 | $w = 0.0001 if $w < 0.0001; |
1703 | $w = 0.0001 if $w < 0.0001; |
1532 | $w = 5 if $w > 5; |
1704 | $w = 5 if $w > 5; |
1533 | |
1705 | |
1534 | $w = AnyEvent->timer (after => $w, cb => $rcb); |
1706 | $w = AE::timer $w, 0, $rcb; |
1535 | } else { |
1707 | } else { |
1536 | # clean up... |
1708 | # clean up... |
1537 | undef $w; |
1709 | undef $w; |
1538 | undef $rcb; |
1710 | undef $rcb; |
|
|
1711 | } |
|
|
1712 | }; |
|
|
1713 | |
|
|
1714 | $w = AE::timer 0.05, 0, $rcb; |
|
|
1715 | |
|
|
1716 | bless \\$cb, "AnyEvent::Base::idle" |
1539 | } |
1717 | }; |
|
|
1718 | |
|
|
1719 | *AnyEvent::Base::idle::DESTROY = sub { |
|
|
1720 | undef $${$_[0]}; |
|
|
1721 | }; |
1540 | }; |
1722 | }; |
|
|
1723 | die if $@; |
1541 | |
1724 | |
1542 | $w = AnyEvent->timer (after => 0.05, cb => $rcb); |
1725 | &idle |
1543 | |
|
|
1544 | bless \\$cb, "AnyEvent::Base::idle" |
|
|
1545 | } |
|
|
1546 | |
|
|
1547 | sub AnyEvent::Base::idle::DESTROY { |
|
|
1548 | undef $${$_[0]}; |
|
|
1549 | } |
1726 | } |
1550 | |
1727 | |
1551 | package AnyEvent::CondVar; |
1728 | package AnyEvent::CondVar; |
1552 | |
1729 | |
1553 | our @ISA = AnyEvent::CondVar::Base::; |
1730 | our @ISA = AnyEvent::CondVar::Base::; |
|
|
1731 | |
|
|
1732 | # only to be used for subclassing |
|
|
1733 | sub new { |
|
|
1734 | my $class = shift; |
|
|
1735 | bless AnyEvent->condvar (@_), $class |
|
|
1736 | } |
1554 | |
1737 | |
1555 | package AnyEvent::CondVar::Base; |
1738 | package AnyEvent::CondVar::Base; |
1556 | |
1739 | |
1557 | #use overload |
1740 | #use overload |
1558 | # '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
1741 | # '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
… | |
… | |
1601 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1784 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1602 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1785 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1603 | } |
1786 | } |
1604 | |
1787 | |
1605 | sub cb { |
1788 | sub cb { |
1606 | $_[0]{_ae_cb} = $_[1] if @_ > 1; |
1789 | my $cv = shift; |
|
|
1790 | |
|
|
1791 | @_ |
|
|
1792 | and $cv->{_ae_cb} = shift |
|
|
1793 | and $cv->{_ae_sent} |
|
|
1794 | and (delete $cv->{_ae_cb})->($cv); |
|
|
1795 | |
1607 | $_[0]{_ae_cb} |
1796 | $cv->{_ae_cb} |
1608 | } |
1797 | } |
1609 | |
1798 | |
1610 | sub begin { |
1799 | sub begin { |
1611 | ++$_[0]{_ae_counter}; |
1800 | ++$_[0]{_ae_counter}; |
1612 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
1801 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
… | |
… | |
1674 | check the arguments passed to most method calls. If it finds any problems, |
1863 | check the arguments passed to most method calls. If it finds any problems, |
1675 | it will croak. |
1864 | it will croak. |
1676 | |
1865 | |
1677 | In other words, enables "strict" mode. |
1866 | In other words, enables "strict" mode. |
1678 | |
1867 | |
1679 | Unlike C<use strict> (or it's modern cousin, C<< use L<common::sense> |
1868 | Unlike C<use strict> (or its modern cousin, C<< use L<common::sense> |
1680 | >>, it is definitely recommended to keep it off in production. Keeping |
1869 | >>, it is definitely recommended to keep it off in production. Keeping |
1681 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
1870 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
1682 | can be very useful, however. |
1871 | can be very useful, however. |
1683 | |
1872 | |
1684 | =item C<PERL_ANYEVENT_MODEL> |
1873 | =item C<PERL_ANYEVENT_MODEL> |
… | |
… | |
1821 | warn "read: $input\n"; # output what has been read |
2010 | warn "read: $input\n"; # output what has been read |
1822 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
2011 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1823 | }, |
2012 | }, |
1824 | ); |
2013 | ); |
1825 | |
2014 | |
1826 | my $time_watcher; # can only be used once |
|
|
1827 | |
|
|
1828 | sub new_timer { |
|
|
1829 | $timer = AnyEvent->timer (after => 1, cb => sub { |
2015 | my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub { |
1830 | warn "timeout\n"; # print 'timeout' about every second |
2016 | warn "timeout\n"; # print 'timeout' at most every second |
1831 | &new_timer; # and restart the time |
|
|
1832 | }); |
2017 | }); |
1833 | } |
|
|
1834 | |
|
|
1835 | new_timer; # create first timer |
|
|
1836 | |
2018 | |
1837 | $cv->recv; # wait until user enters /^q/i |
2019 | $cv->recv; # wait until user enters /^q/i |
1838 | |
2020 | |
1839 | =head1 REAL-WORLD EXAMPLE |
2021 | =head1 REAL-WORLD EXAMPLE |
1840 | |
2022 | |
… | |
… | |
1913 | |
2095 | |
1914 | The actual code goes further and collects all errors (C<die>s, exceptions) |
2096 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1915 | that occurred during request processing. The C<result> method detects |
2097 | that occurred during request processing. The C<result> method detects |
1916 | whether an exception as thrown (it is stored inside the $txn object) |
2098 | whether an exception as thrown (it is stored inside the $txn object) |
1917 | and just throws the exception, which means connection errors and other |
2099 | and just throws the exception, which means connection errors and other |
1918 | problems get reported tot he code that tries to use the result, not in a |
2100 | problems get reported to the code that tries to use the result, not in a |
1919 | random callback. |
2101 | random callback. |
1920 | |
2102 | |
1921 | All of this enables the following usage styles: |
2103 | All of this enables the following usage styles: |
1922 | |
2104 | |
1923 | 1. Blocking: |
2105 | 1. Blocking: |
… | |
… | |
1971 | through AnyEvent. The benchmark creates a lot of timers (with a zero |
2153 | through AnyEvent. The benchmark creates a lot of timers (with a zero |
1972 | timeout) and I/O watchers (watching STDOUT, a pty, to become writable, |
2154 | timeout) and I/O watchers (watching STDOUT, a pty, to become writable, |
1973 | which it is), lets them fire exactly once and destroys them again. |
2155 | which it is), lets them fire exactly once and destroys them again. |
1974 | |
2156 | |
1975 | Source code for this benchmark is found as F<eg/bench> in the AnyEvent |
2157 | Source code for this benchmark is found as F<eg/bench> in the AnyEvent |
1976 | distribution. |
2158 | distribution. It uses the L<AE> interface, which makes a real difference |
|
|
2159 | for the EV and Perl backends only. |
1977 | |
2160 | |
1978 | =head3 Explanation of the columns |
2161 | =head3 Explanation of the columns |
1979 | |
2162 | |
1980 | I<watcher> is the number of event watchers created/destroyed. Since |
2163 | I<watcher> is the number of event watchers created/destroyed. Since |
1981 | different event models feature vastly different performances, each event |
2164 | different event models feature vastly different performances, each event |
… | |
… | |
2002 | watcher. |
2185 | watcher. |
2003 | |
2186 | |
2004 | =head3 Results |
2187 | =head3 Results |
2005 | |
2188 | |
2006 | name watchers bytes create invoke destroy comment |
2189 | name watchers bytes create invoke destroy comment |
2007 | EV/EV 400000 224 0.47 0.35 0.27 EV native interface |
2190 | EV/EV 100000 223 0.47 0.43 0.27 EV native interface |
2008 | EV/Any 100000 224 2.88 0.34 0.27 EV + AnyEvent watchers |
2191 | EV/Any 100000 223 0.48 0.42 0.26 EV + AnyEvent watchers |
2009 | CoroEV/Any 100000 224 2.85 0.35 0.28 coroutines + Coro::Signal |
2192 | Coro::EV/Any 100000 223 0.47 0.42 0.26 coroutines + Coro::Signal |
2010 | Perl/Any 100000 452 4.13 0.73 0.95 pure perl implementation |
2193 | Perl/Any 100000 431 2.70 0.74 0.92 pure perl implementation |
2011 | Event/Event 16000 517 32.20 31.80 0.81 Event native interface |
2194 | Event/Event 16000 516 31.16 31.84 0.82 Event native interface |
2012 | Event/Any 16000 590 35.85 31.55 1.06 Event + AnyEvent watchers |
2195 | Event/Any 16000 1203 42.61 34.79 1.80 Event + AnyEvent watchers |
2013 | IOAsync/Any 16000 989 38.10 32.77 11.13 via IO::Async::Loop::IO_Poll |
2196 | IOAsync/Any 16000 1911 41.92 27.45 16.81 via IO::Async::Loop::IO_Poll |
2014 | IOAsync/Any 16000 990 37.59 29.50 10.61 via IO::Async::Loop::Epoll |
2197 | IOAsync/Any 16000 1726 40.69 26.37 15.25 via IO::Async::Loop::Epoll |
2015 | Glib/Any 16000 1357 102.33 12.31 51.00 quadratic behaviour |
2198 | Glib/Any 16000 1118 89.00 12.57 51.17 quadratic behaviour |
2016 | Tk/Any 2000 1860 27.20 66.31 14.00 SEGV with >> 2000 watchers |
2199 | Tk/Any 2000 1346 20.96 10.75 8.00 SEGV with >> 2000 watchers |
2017 | POE/Event 2000 6328 109.99 751.67 14.02 via POE::Loop::Event |
2200 | POE/Any 2000 6951 108.97 795.32 14.24 via POE::Loop::Event |
2018 | POE/Select 2000 6027 94.54 809.13 579.80 via POE::Loop::Select |
2201 | POE/Any 2000 6648 94.79 774.40 575.51 via POE::Loop::Select |
2019 | |
2202 | |
2020 | =head3 Discussion |
2203 | =head3 Discussion |
2021 | |
2204 | |
2022 | The benchmark does I<not> measure scalability of the event loop very |
2205 | The benchmark does I<not> measure scalability of the event loop very |
2023 | well. For example, a select-based event loop (such as the pure perl one) |
2206 | well. For example, a select-based event loop (such as the pure perl one) |
… | |
… | |
2035 | benchmark machine, handling an event takes roughly 1600 CPU cycles with |
2218 | benchmark machine, handling an event takes roughly 1600 CPU cycles with |
2036 | EV, 3100 CPU cycles with AnyEvent's pure perl loop and almost 3000000 CPU |
2219 | EV, 3100 CPU cycles with AnyEvent's pure perl loop and almost 3000000 CPU |
2037 | cycles with POE. |
2220 | cycles with POE. |
2038 | |
2221 | |
2039 | C<EV> is the sole leader regarding speed and memory use, which are both |
2222 | C<EV> is the sole leader regarding speed and memory use, which are both |
2040 | maximal/minimal, respectively. Even when going through AnyEvent, it uses |
2223 | maximal/minimal, respectively. When using the L<AE> API there is zero |
|
|
2224 | overhead (when going through the AnyEvent API create is about 5-6 times |
|
|
2225 | slower, with other times being equal, so still uses far less memory than |
2041 | far less memory than any other event loop and is still faster than Event |
2226 | any other event loop and is still faster than Event natively). |
2042 | natively. |
|
|
2043 | |
2227 | |
2044 | The pure perl implementation is hit in a few sweet spots (both the |
2228 | The pure perl implementation is hit in a few sweet spots (both the |
2045 | constant timeout and the use of a single fd hit optimisations in the perl |
2229 | constant timeout and the use of a single fd hit optimisations in the perl |
2046 | interpreter and the backend itself). Nevertheless this shows that it |
2230 | interpreter and the backend itself). Nevertheless this shows that it |
2047 | adds very little overhead in itself. Like any select-based backend its |
2231 | adds very little overhead in itself. Like any select-based backend its |
… | |
… | |
2121 | In this benchmark, we use 10000 socket pairs (20000 sockets), of which 100 |
2305 | In this benchmark, we use 10000 socket pairs (20000 sockets), of which 100 |
2122 | (1%) are active. This mirrors the activity of large servers with many |
2306 | (1%) are active. This mirrors the activity of large servers with many |
2123 | connections, most of which are idle at any one point in time. |
2307 | connections, most of which are idle at any one point in time. |
2124 | |
2308 | |
2125 | Source code for this benchmark is found as F<eg/bench2> in the AnyEvent |
2309 | Source code for this benchmark is found as F<eg/bench2> in the AnyEvent |
2126 | distribution. |
2310 | distribution. It uses the L<AE> interface, which makes a real difference |
|
|
2311 | for the EV and Perl backends only. |
2127 | |
2312 | |
2128 | =head3 Explanation of the columns |
2313 | =head3 Explanation of the columns |
2129 | |
2314 | |
2130 | I<sockets> is the number of sockets, and twice the number of "servers" (as |
2315 | I<sockets> is the number of sockets, and twice the number of "servers" (as |
2131 | each server has a read and write socket end). |
2316 | each server has a read and write socket end). |
… | |
… | |
2139 | a new one that moves the timeout into the future. |
2324 | a new one that moves the timeout into the future. |
2140 | |
2325 | |
2141 | =head3 Results |
2326 | =head3 Results |
2142 | |
2327 | |
2143 | name sockets create request |
2328 | name sockets create request |
2144 | EV 20000 69.01 11.16 |
2329 | EV 20000 62.66 7.99 |
2145 | Perl 20000 73.32 35.87 |
2330 | Perl 20000 68.32 32.64 |
2146 | IOAsync 20000 157.00 98.14 epoll |
2331 | IOAsync 20000 174.06 101.15 epoll |
2147 | IOAsync 20000 159.31 616.06 poll |
2332 | IOAsync 20000 174.67 610.84 poll |
2148 | Event 20000 212.62 257.32 |
2333 | Event 20000 202.69 242.91 |
2149 | Glib 20000 651.16 1896.30 |
2334 | Glib 20000 557.01 1689.52 |
2150 | POE 20000 349.67 12317.24 uses POE::Loop::Event |
2335 | POE 20000 341.54 12086.32 uses POE::Loop::Event |
2151 | |
2336 | |
2152 | =head3 Discussion |
2337 | =head3 Discussion |
2153 | |
2338 | |
2154 | This benchmark I<does> measure scalability and overall performance of the |
2339 | This benchmark I<does> measure scalability and overall performance of the |
2155 | particular event loop. |
2340 | particular event loop. |
… | |
… | |
2281 | As you can see, the AnyEvent + EV combination even beats the |
2466 | As you can see, the AnyEvent + EV combination even beats the |
2282 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
2467 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
2283 | backend easily beats IO::Lambda and POE. |
2468 | backend easily beats IO::Lambda and POE. |
2284 | |
2469 | |
2285 | And even the 100% non-blocking version written using the high-level (and |
2470 | And even the 100% non-blocking version written using the high-level (and |
2286 | slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda by a |
2471 | slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda |
2287 | large margin, even though it does all of DNS, tcp-connect and socket I/O |
2472 | higher level ("unoptimised") abstractions by a large margin, even though |
2288 | in a non-blocking way. |
2473 | it does all of DNS, tcp-connect and socket I/O in a non-blocking way. |
2289 | |
2474 | |
2290 | The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and |
2475 | The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and |
2291 | F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are |
2476 | F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are |
2292 | part of the IO::lambda distribution and were used without any changes. |
2477 | part of the IO::Lambda distribution and were used without any changes. |
2293 | |
2478 | |
2294 | |
2479 | |
2295 | =head1 SIGNALS |
2480 | =head1 SIGNALS |
2296 | |
2481 | |
2297 | AnyEvent currently installs handlers for these signals: |
2482 | AnyEvent currently installs handlers for these signals: |
… | |
… | |
2334 | unless defined $SIG{PIPE}; |
2519 | unless defined $SIG{PIPE}; |
2335 | |
2520 | |
2336 | =head1 RECOMMENDED/OPTIONAL MODULES |
2521 | =head1 RECOMMENDED/OPTIONAL MODULES |
2337 | |
2522 | |
2338 | One of AnyEvent's main goals is to be 100% Pure-Perl(tm): only perl (and |
2523 | One of AnyEvent's main goals is to be 100% Pure-Perl(tm): only perl (and |
2339 | it's built-in modules) are required to use it. |
2524 | its built-in modules) are required to use it. |
2340 | |
2525 | |
2341 | That does not mean that AnyEvent won't take advantage of some additional |
2526 | That does not mean that AnyEvent won't take advantage of some additional |
2342 | modules if they are installed. |
2527 | modules if they are installed. |
2343 | |
2528 | |
2344 | This section epxlains which additional modules will be used, and how they |
2529 | This section explains which additional modules will be used, and how they |
2345 | affect AnyEvent's operetion. |
2530 | affect AnyEvent's operation. |
2346 | |
2531 | |
2347 | =over 4 |
2532 | =over 4 |
2348 | |
2533 | |
2349 | =item L<Async::Interrupt> |
2534 | =item L<Async::Interrupt> |
2350 | |
2535 | |
… | |
… | |
2355 | catch the signals) with some delay (default is 10 seconds, look for |
2540 | catch the signals) with some delay (default is 10 seconds, look for |
2356 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2541 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2357 | |
2542 | |
2358 | If this module is available, then it will be used to implement signal |
2543 | If this module is available, then it will be used to implement signal |
2359 | catching, which means that signals will not be delayed, and the event loop |
2544 | catching, which means that signals will not be delayed, and the event loop |
2360 | will not be interrupted regularly, which is more efficient (And good for |
2545 | will not be interrupted regularly, which is more efficient (and good for |
2361 | battery life on laptops). |
2546 | battery life on laptops). |
2362 | |
2547 | |
2363 | This affects not just the pure-perl event loop, but also other event loops |
2548 | This affects not just the pure-perl event loop, but also other event loops |
2364 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
2549 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
2365 | |
2550 | |
… | |
… | |
2377 | automatic timer adjustments even when no monotonic clock is available, |
2562 | automatic timer adjustments even when no monotonic clock is available, |
2378 | can take avdantage of advanced kernel interfaces such as C<epoll> and |
2563 | can take avdantage of advanced kernel interfaces such as C<epoll> and |
2379 | C<kqueue>, and is the fastest backend I<by far>. You can even embed |
2564 | C<kqueue>, and is the fastest backend I<by far>. You can even embed |
2380 | L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). |
2565 | L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). |
2381 | |
2566 | |
|
|
2567 | If you only use backends that rely on another event loop (e.g. C<Tk>), |
|
|
2568 | then this module will do nothing for you. |
|
|
2569 | |
2382 | =item L<Guard> |
2570 | =item L<Guard> |
2383 | |
2571 | |
2384 | The guard module, when used, will be used to implement |
2572 | The guard module, when used, will be used to implement |
2385 | C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a |
2573 | C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a |
2386 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2574 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2387 | purely used for performance. |
2575 | purely used for performance. |
2388 | |
2576 | |
2389 | =item L<JSON> and L<JSON::XS> |
2577 | =item L<JSON> and L<JSON::XS> |
2390 | |
2578 | |
2391 | This module is required when you want to read or write JSON data via |
2579 | One of these modules is required when you want to read or write JSON data |
2392 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2580 | via L<AnyEvent::Handle>. L<JSON> is also written in pure-perl, but can take |
2393 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2581 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2394 | |
|
|
2395 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
|
|
2396 | installed. |
|
|
2397 | |
2582 | |
2398 | =item L<Net::SSLeay> |
2583 | =item L<Net::SSLeay> |
2399 | |
2584 | |
2400 | Implementing TLS/SSL in Perl is certainly interesting, but not very |
2585 | Implementing TLS/SSL in Perl is certainly interesting, but not very |
2401 | worthwhile: If this module is installed, then L<AnyEvent::Handle> (with |
2586 | worthwhile: If this module is installed, then L<AnyEvent::Handle> (with |
2402 | the help of L<AnyEvent::TLS>), gains the ability to do TLS/SSL. |
2587 | the help of L<AnyEvent::TLS>), gains the ability to do TLS/SSL. |
2403 | |
2588 | |
2404 | =item L<Time::HiRes> |
2589 | =item L<Time::HiRes> |
2405 | |
2590 | |
2406 | This module is part of perl since release 5.008. It will be used when the |
2591 | This module is part of perl since release 5.008. It will be used when the |
2407 | chosen event library does not come with a timing source on it's own. The |
2592 | chosen event library does not come with a timing source of its own. The |
2408 | pure-perl event loop (L<AnyEvent::Impl::Perl>) will additionally use it to |
2593 | pure-perl event loop (L<AnyEvent::Impl::Perl>) will additionally use it to |
2409 | try to use a monotonic clock for timing stability. |
2594 | try to use a monotonic clock for timing stability. |
2410 | |
2595 | |
2411 | =back |
2596 | =back |
2412 | |
2597 | |
2413 | |
2598 | |
2414 | =head1 FORK |
2599 | =head1 FORK |
2415 | |
2600 | |
2416 | Most event libraries are not fork-safe. The ones who are usually are |
2601 | Most event libraries are not fork-safe. The ones who are usually are |
2417 | because they rely on inefficient but fork-safe C<select> or C<poll> |
2602 | because they rely on inefficient but fork-safe C<select> or C<poll> calls |
2418 | calls. Only L<EV> is fully fork-aware. |
2603 | - higher performance APIs such as BSD's kqueue or the dreaded Linux epoll |
|
|
2604 | are usually badly thought-out hacks that are incompatible with fork in |
|
|
2605 | one way or another. Only L<EV> is fully fork-aware and ensures that you |
|
|
2606 | continue event-processing in both parent and child (or both, if you know |
|
|
2607 | what you are doing). |
|
|
2608 | |
|
|
2609 | This means that, in general, you cannot fork and do event processing in |
|
|
2610 | the child if the event library was initialised before the fork (which |
|
|
2611 | usually happens when the first AnyEvent watcher is created, or the library |
|
|
2612 | is loaded). |
2419 | |
2613 | |
2420 | If you have to fork, you must either do so I<before> creating your first |
2614 | If you have to fork, you must either do so I<before> creating your first |
2421 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2615 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2422 | something completely out of the scope of AnyEvent. |
2616 | something completely out of the scope of AnyEvent. |
|
|
2617 | |
|
|
2618 | The problem of doing event processing in the parent I<and> the child |
|
|
2619 | is much more complicated: even for backends that I<are> fork-aware or |
|
|
2620 | fork-safe, their behaviour is not usually what you want: fork clones all |
|
|
2621 | watchers, that means all timers, I/O watchers etc. are active in both |
|
|
2622 | parent and child, which is almost never what you want. USing C<exec> |
|
|
2623 | to start worker children from some kind of manage rprocess is usually |
|
|
2624 | preferred, because it is much easier and cleaner, at the expense of having |
|
|
2625 | to have another binary. |
2423 | |
2626 | |
2424 | |
2627 | |
2425 | =head1 SECURITY CONSIDERATIONS |
2628 | =head1 SECURITY CONSIDERATIONS |
2426 | |
2629 | |
2427 | AnyEvent can be forced to load any event model via |
2630 | AnyEvent can be forced to load any event model via |
… | |
… | |
2457 | pronounced). |
2660 | pronounced). |
2458 | |
2661 | |
2459 | |
2662 | |
2460 | =head1 SEE ALSO |
2663 | =head1 SEE ALSO |
2461 | |
2664 | |
|
|
2665 | Tutorial/Introduction: L<AnyEvent::Intro>. |
|
|
2666 | |
|
|
2667 | FAQ: L<AnyEvent::FAQ>. |
|
|
2668 | |
2462 | Utility functions: L<AnyEvent::Util>. |
2669 | Utility functions: L<AnyEvent::Util>. |
2463 | |
2670 | |
2464 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
2671 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
2465 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2672 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2466 | |
2673 | |
… | |
… | |
2472 | Non-blocking file handles, sockets, TCP clients and |
2679 | Non-blocking file handles, sockets, TCP clients and |
2473 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2680 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2474 | |
2681 | |
2475 | Asynchronous DNS: L<AnyEvent::DNS>. |
2682 | Asynchronous DNS: L<AnyEvent::DNS>. |
2476 | |
2683 | |
2477 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, |
2684 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
2478 | L<Coro::Event>, |
|
|
2479 | |
2685 | |
2480 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::XMPP>, |
2686 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::IRC>, |
2481 | L<AnyEvent::HTTP>. |
2687 | L<AnyEvent::HTTP>. |
2482 | |
2688 | |
2483 | |
2689 | |
2484 | =head1 AUTHOR |
2690 | =head1 AUTHOR |
2485 | |
2691 | |