1 | =head1 => NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - provide framework for multiple event loops |
3 | AnyEvent - provide framework for multiple event loops |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { |
11 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... }); |
12 | ... |
|
|
13 | }); |
|
|
14 | |
12 | |
15 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
13 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
|
|
14 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
|
|
15 | |
|
|
16 | print AnyEvent->now; # prints current event loop time |
|
|
17 | print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. |
|
|
18 | |
|
|
19 | my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); |
|
|
20 | |
|
|
21 | my $w = AnyEvent->child (pid => $pid, cb => sub { |
|
|
22 | my ($pid, $status) = @_; |
16 | ... |
23 | ... |
17 | }); |
24 | }); |
18 | |
25 | |
19 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
26 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
20 | $w->send; # wake up current and all future recv's |
27 | $w->send; # wake up current and all future recv's |
21 | $w->recv; # enters "main loop" till $condvar gets ->send |
28 | $w->recv; # enters "main loop" till $condvar gets ->send |
|
|
29 | # use a condvar in callback mode: |
|
|
30 | $w->cb (sub { $_[0]->recv }); |
22 | |
31 | |
23 | =head1 INTRODUCTION/TUTORIAL |
32 | =head1 INTRODUCTION/TUTORIAL |
24 | |
33 | |
25 | This manpage is mainly a reference manual. If you are interested |
34 | This manpage is mainly a reference manual. If you are interested |
26 | in a tutorial or some gentle introduction, have a look at the |
35 | in a tutorial or some gentle introduction, have a look at the |
… | |
… | |
33 | |
42 | |
34 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
43 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
35 | policy> and AnyEvent is I<small and efficient>. |
44 | policy> and AnyEvent is I<small and efficient>. |
36 | |
45 | |
37 | First and foremost, I<AnyEvent is not an event model> itself, it only |
46 | First and foremost, I<AnyEvent is not an event model> itself, it only |
38 | interfaces to whatever event model the main program happens to use in a |
47 | interfaces to whatever event model the main program happens to use, in a |
39 | pragmatic way. For event models and certain classes of immortals alike, |
48 | pragmatic way. For event models and certain classes of immortals alike, |
40 | the statement "there can only be one" is a bitter reality: In general, |
49 | the statement "there can only be one" is a bitter reality: In general, |
41 | only one event loop can be active at the same time in a process. AnyEvent |
50 | only one event loop can be active at the same time in a process. AnyEvent |
42 | helps hiding the differences between those event loops. |
51 | cannot change this, but it can hide the differences between those event |
|
|
52 | loops. |
43 | |
53 | |
44 | The goal of AnyEvent is to offer module authors the ability to do event |
54 | The goal of AnyEvent is to offer module authors the ability to do event |
45 | programming (waiting for I/O or timer events) without subscribing to a |
55 | programming (waiting for I/O or timer events) without subscribing to a |
46 | religion, a way of living, and most importantly: without forcing your |
56 | religion, a way of living, and most importantly: without forcing your |
47 | module users into the same thing by forcing them to use the same event |
57 | module users into the same thing by forcing them to use the same event |
48 | model you use. |
58 | model you use. |
49 | |
59 | |
50 | For modules like POE or IO::Async (which is a total misnomer as it is |
60 | For modules like POE or IO::Async (which is a total misnomer as it is |
51 | actually doing all I/O I<synchronously>...), using them in your module is |
61 | actually doing all I/O I<synchronously>...), using them in your module is |
52 | like joining a cult: After you joined, you are dependent on them and you |
62 | like joining a cult: After you joined, you are dependent on them and you |
53 | cannot use anything else, as it is simply incompatible to everything that |
63 | cannot use anything else, as they are simply incompatible to everything |
54 | isn't itself. What's worse, all the potential users of your module are |
64 | that isn't them. What's worse, all the potential users of your |
55 | I<also> forced to use the same event loop you use. |
65 | module are I<also> forced to use the same event loop you use. |
56 | |
66 | |
57 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
67 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
58 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
68 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
59 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
69 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
60 | your module uses one of those, every user of your module has to use it, |
70 | your module uses one of those, every user of your module has to use it, |
61 | too. But if your module uses AnyEvent, it works transparently with all |
71 | too. But if your module uses AnyEvent, it works transparently with all |
62 | event models it supports (including stuff like POE and IO::Async, as long |
72 | event models it supports (including stuff like IO::Async, as long as those |
63 | as those use one of the supported event loops. It is trivial to add new |
73 | use one of the supported event loops. It is trivial to add new event loops |
64 | event loops to AnyEvent, too, so it is future-proof). |
74 | to AnyEvent, too, so it is future-proof). |
65 | |
75 | |
66 | In addition to being free of having to use I<the one and only true event |
76 | In addition to being free of having to use I<the one and only true event |
67 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
77 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
68 | modules, you get an enormous amount of code and strict rules you have to |
78 | modules, you get an enormous amount of code and strict rules you have to |
69 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
79 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
… | |
… | |
127 | These watchers are normal Perl objects with normal Perl lifetime. After |
137 | These watchers are normal Perl objects with normal Perl lifetime. After |
128 | creating a watcher it will immediately "watch" for events and invoke the |
138 | creating a watcher it will immediately "watch" for events and invoke the |
129 | callback when the event occurs (of course, only when the event model |
139 | callback when the event occurs (of course, only when the event model |
130 | is in control). |
140 | is in control). |
131 | |
141 | |
|
|
142 | Note that B<callbacks must not permanently change global variables> |
|
|
143 | potentially in use by the event loop (such as C<$_> or C<$[>) and that B<< |
|
|
144 | callbacks must not C<die> >>. The former is good programming practise in |
|
|
145 | Perl and the latter stems from the fact that exception handling differs |
|
|
146 | widely between event loops. |
|
|
147 | |
132 | To disable the watcher you have to destroy it (e.g. by setting the |
148 | To disable the watcher you have to destroy it (e.g. by setting the |
133 | variable you store it in to C<undef> or otherwise deleting all references |
149 | variable you store it in to C<undef> or otherwise deleting all references |
134 | to it). |
150 | to it). |
135 | |
151 | |
136 | All watchers are created by calling a method on the C<AnyEvent> class. |
152 | All watchers are created by calling a method on the C<AnyEvent> class. |
… | |
… | |
138 | Many watchers either are used with "recursion" (repeating timers for |
154 | Many watchers either are used with "recursion" (repeating timers for |
139 | example), or need to refer to their watcher object in other ways. |
155 | example), or need to refer to their watcher object in other ways. |
140 | |
156 | |
141 | An any way to achieve that is this pattern: |
157 | An any way to achieve that is this pattern: |
142 | |
158 | |
143 | my $w; $w = AnyEvent->type (arg => value ..., cb => sub { |
159 | my $w; $w = AnyEvent->type (arg => value ..., cb => sub { |
144 | # you can use $w here, for example to undef it |
160 | # you can use $w here, for example to undef it |
145 | undef $w; |
161 | undef $w; |
146 | }); |
162 | }); |
147 | |
163 | |
148 | Note that C<my $w; $w => combination. This is necessary because in Perl, |
164 | Note that C<my $w; $w => combination. This is necessary because in Perl, |
149 | my variables are only visible after the statement in which they are |
165 | my variables are only visible after the statement in which they are |
150 | declared. |
166 | declared. |
151 | |
167 | |
152 | =head2 I/O WATCHERS |
168 | =head2 I/O WATCHERS |
153 | |
169 | |
154 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
170 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
155 | with the following mandatory key-value pairs as arguments: |
171 | with the following mandatory key-value pairs as arguments: |
156 | |
172 | |
157 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch |
173 | C<fh> is the Perl I<file handle> (I<not> file descriptor) to watch |
|
|
174 | for events (AnyEvent might or might not keep a reference to this file |
|
|
175 | handle). Note that only file handles pointing to things for which |
|
|
176 | non-blocking operation makes sense are allowed. This includes sockets, |
|
|
177 | most character devices, pipes, fifos and so on, but not for example files |
|
|
178 | or block devices. |
|
|
179 | |
158 | for events. C<poll> must be a string that is either C<r> or C<w>, |
180 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
159 | which creates a watcher waiting for "r"eadable or "w"ritable events, |
181 | watcher waiting for "r"eadable or "w"ritable events, respectively. |
|
|
182 | |
160 | respectively. C<cb> is the callback to invoke each time the file handle |
183 | C<cb> is the callback to invoke each time the file handle becomes ready. |
161 | becomes ready. |
|
|
162 | |
184 | |
163 | Although the callback might get passed parameters, their value and |
185 | Although the callback might get passed parameters, their value and |
164 | presence is undefined and you cannot rely on them. Portable AnyEvent |
186 | presence is undefined and you cannot rely on them. Portable AnyEvent |
165 | callbacks cannot use arguments passed to I/O watcher callbacks. |
187 | callbacks cannot use arguments passed to I/O watcher callbacks. |
166 | |
188 | |
… | |
… | |
170 | |
192 | |
171 | Some event loops issue spurious readyness notifications, so you should |
193 | Some event loops issue spurious readyness notifications, so you should |
172 | always use non-blocking calls when reading/writing from/to your file |
194 | always use non-blocking calls when reading/writing from/to your file |
173 | handles. |
195 | handles. |
174 | |
196 | |
175 | Example: |
|
|
176 | |
|
|
177 | # wait for readability of STDIN, then read a line and disable the watcher |
197 | Example: wait for readability of STDIN, then read a line and disable the |
|
|
198 | watcher. |
|
|
199 | |
178 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
200 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
179 | chomp (my $input = <STDIN>); |
201 | chomp (my $input = <STDIN>); |
180 | warn "read: $input\n"; |
202 | warn "read: $input\n"; |
181 | undef $w; |
203 | undef $w; |
182 | }); |
204 | }); |
… | |
… | |
192 | |
214 | |
193 | Although the callback might get passed parameters, their value and |
215 | Although the callback might get passed parameters, their value and |
194 | presence is undefined and you cannot rely on them. Portable AnyEvent |
216 | presence is undefined and you cannot rely on them. Portable AnyEvent |
195 | callbacks cannot use arguments passed to time watcher callbacks. |
217 | callbacks cannot use arguments passed to time watcher callbacks. |
196 | |
218 | |
197 | The timer callback will be invoked at most once: if you want a repeating |
219 | The callback will normally be invoked once only. If you specify another |
198 | timer you have to create a new watcher (this is a limitation by both Tk |
220 | parameter, C<interval>, as a strictly positive number (> 0), then the |
199 | and Glib). |
221 | callback will be invoked regularly at that interval (in fractional |
|
|
222 | seconds) after the first invocation. If C<interval> is specified with a |
|
|
223 | false value, then it is treated as if it were missing. |
200 | |
224 | |
201 | Example: |
225 | The callback will be rescheduled before invoking the callback, but no |
|
|
226 | attempt is done to avoid timer drift in most backends, so the interval is |
|
|
227 | only approximate. |
202 | |
228 | |
203 | # fire an event after 7.7 seconds |
229 | Example: fire an event after 7.7 seconds. |
|
|
230 | |
204 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
231 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
205 | warn "timeout\n"; |
232 | warn "timeout\n"; |
206 | }); |
233 | }); |
207 | |
234 | |
208 | # to cancel the timer: |
235 | # to cancel the timer: |
209 | undef $w; |
236 | undef $w; |
210 | |
237 | |
211 | Example 2: |
|
|
212 | |
|
|
213 | # fire an event after 0.5 seconds, then roughly every second |
238 | Example 2: fire an event after 0.5 seconds, then roughly every second. |
214 | my $w; |
|
|
215 | |
239 | |
216 | my $cb = sub { |
|
|
217 | # cancel the old timer while creating a new one |
|
|
218 | $w = AnyEvent->timer (after => 1, cb => $cb); |
240 | my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { |
|
|
241 | warn "timeout\n"; |
219 | }; |
242 | }; |
220 | |
|
|
221 | # start the "loop" by creating the first watcher |
|
|
222 | $w = AnyEvent->timer (after => 0.5, cb => $cb); |
|
|
223 | |
243 | |
224 | =head3 TIMING ISSUES |
244 | =head3 TIMING ISSUES |
225 | |
245 | |
226 | There are two ways to handle timers: based on real time (relative, "fire |
246 | There are two ways to handle timers: based on real time (relative, "fire |
227 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
247 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
… | |
… | |
305 | =back |
325 | =back |
306 | |
326 | |
307 | =head2 SIGNAL WATCHERS |
327 | =head2 SIGNAL WATCHERS |
308 | |
328 | |
309 | You can watch for signals using a signal watcher, C<signal> is the signal |
329 | You can watch for signals using a signal watcher, C<signal> is the signal |
310 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
330 | I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl |
311 | be invoked whenever a signal occurs. |
331 | callback to be invoked whenever a signal occurs. |
312 | |
332 | |
313 | Although the callback might get passed parameters, their value and |
333 | Although the callback might get passed parameters, their value and |
314 | presence is undefined and you cannot rely on them. Portable AnyEvent |
334 | presence is undefined and you cannot rely on them. Portable AnyEvent |
315 | callbacks cannot use arguments passed to signal watcher callbacks. |
335 | callbacks cannot use arguments passed to signal watcher callbacks. |
316 | |
336 | |
… | |
… | |
332 | =head2 CHILD PROCESS WATCHERS |
352 | =head2 CHILD PROCESS WATCHERS |
333 | |
353 | |
334 | You can also watch on a child process exit and catch its exit status. |
354 | You can also watch on a child process exit and catch its exit status. |
335 | |
355 | |
336 | The child process is specified by the C<pid> argument (if set to C<0>, it |
356 | The child process is specified by the C<pid> argument (if set to C<0>, it |
337 | watches for any child process exit). The watcher will trigger as often |
357 | watches for any child process exit). The watcher will triggered only when |
338 | as status change for the child are received. This works by installing a |
358 | the child process has finished and an exit status is available, not on |
339 | signal handler for C<SIGCHLD>. The callback will be called with the pid |
359 | any trace events (stopped/continued). |
340 | and exit status (as returned by waitpid), so unlike other watcher types, |
360 | |
341 | you I<can> rely on child watcher callback arguments. |
361 | The callback will be called with the pid and exit status (as returned by |
|
|
362 | waitpid), so unlike other watcher types, you I<can> rely on child watcher |
|
|
363 | callback arguments. |
|
|
364 | |
|
|
365 | This watcher type works by installing a signal handler for C<SIGCHLD>, |
|
|
366 | and since it cannot be shared, nothing else should use SIGCHLD or reap |
|
|
367 | random child processes (waiting for specific child processes, e.g. inside |
|
|
368 | C<system>, is just fine). |
342 | |
369 | |
343 | There is a slight catch to child watchers, however: you usually start them |
370 | There is a slight catch to child watchers, however: you usually start them |
344 | I<after> the child process was created, and this means the process could |
371 | I<after> the child process was created, and this means the process could |
345 | have exited already (and no SIGCHLD will be sent anymore). |
372 | have exited already (and no SIGCHLD will be sent anymore). |
346 | |
373 | |
… | |
… | |
352 | AnyEvent program, you I<have> to create at least one watcher before you |
379 | AnyEvent program, you I<have> to create at least one watcher before you |
353 | C<fork> the child (alternatively, you can call C<AnyEvent::detect>). |
380 | C<fork> the child (alternatively, you can call C<AnyEvent::detect>). |
354 | |
381 | |
355 | Example: fork a process and wait for it |
382 | Example: fork a process and wait for it |
356 | |
383 | |
357 | my $done = AnyEvent->condvar; |
384 | my $done = AnyEvent->condvar; |
358 | |
385 | |
359 | my $pid = fork or exit 5; |
386 | my $pid = fork or exit 5; |
360 | |
387 | |
361 | my $w = AnyEvent->child ( |
388 | my $w = AnyEvent->child ( |
362 | pid => $pid, |
389 | pid => $pid, |
363 | cb => sub { |
390 | cb => sub { |
364 | my ($pid, $status) = @_; |
391 | my ($pid, $status) = @_; |
365 | warn "pid $pid exited with status $status"; |
392 | warn "pid $pid exited with status $status"; |
366 | $done->send; |
393 | $done->send; |
367 | }, |
394 | }, |
368 | ); |
395 | ); |
369 | |
396 | |
370 | # do something else, then wait for process exit |
397 | # do something else, then wait for process exit |
371 | $done->recv; |
398 | $done->recv; |
372 | |
399 | |
373 | =head2 CONDITION VARIABLES |
400 | =head2 CONDITION VARIABLES |
374 | |
401 | |
375 | If you are familiar with some event loops you will know that all of them |
402 | If you are familiar with some event loops you will know that all of them |
376 | require you to run some blocking "loop", "run" or similar function that |
403 | require you to run some blocking "loop", "run" or similar function that |
… | |
… | |
382 | The instrument to do that is called a "condition variable", so called |
409 | The instrument to do that is called a "condition variable", so called |
383 | because they represent a condition that must become true. |
410 | because they represent a condition that must become true. |
384 | |
411 | |
385 | Condition variables can be created by calling the C<< AnyEvent->condvar |
412 | Condition variables can be created by calling the C<< AnyEvent->condvar |
386 | >> method, usually without arguments. The only argument pair allowed is |
413 | >> method, usually without arguments. The only argument pair allowed is |
|
|
414 | |
387 | C<cb>, which specifies a callback to be called when the condition variable |
415 | C<cb>, which specifies a callback to be called when the condition variable |
388 | becomes true. |
416 | becomes true, with the condition variable as the first argument (but not |
|
|
417 | the results). |
389 | |
418 | |
390 | After creation, the condition variable is "false" until it becomes "true" |
419 | After creation, the condition variable is "false" until it becomes "true" |
391 | by calling the C<send> method (or calling the condition variable as if it |
420 | by calling the C<send> method (or calling the condition variable as if it |
392 | were a callback, read about the caveats in the description for the C<< |
421 | were a callback, read about the caveats in the description for the C<< |
393 | ->send >> method). |
422 | ->send >> method). |
… | |
… | |
449 | |
478 | |
450 | my $done = AnyEvent->condvar; |
479 | my $done = AnyEvent->condvar; |
451 | my $delay = AnyEvent->timer (after => 5, cb => $done); |
480 | my $delay = AnyEvent->timer (after => 5, cb => $done); |
452 | $done->recv; |
481 | $done->recv; |
453 | |
482 | |
|
|
483 | Example: Imagine an API that returns a condvar and doesn't support |
|
|
484 | callbacks. This is how you make a synchronous call, for example from |
|
|
485 | the main program: |
|
|
486 | |
|
|
487 | use AnyEvent::CouchDB; |
|
|
488 | |
|
|
489 | ... |
|
|
490 | |
|
|
491 | my @info = $couchdb->info->recv; |
|
|
492 | |
|
|
493 | And this is how you would just ste a callback to be called whenever the |
|
|
494 | results are available: |
|
|
495 | |
|
|
496 | $couchdb->info->cb (sub { |
|
|
497 | my @info = $_[0]->recv; |
|
|
498 | }); |
|
|
499 | |
454 | =head3 METHODS FOR PRODUCERS |
500 | =head3 METHODS FOR PRODUCERS |
455 | |
501 | |
456 | These methods should only be used by the producing side, i.e. the |
502 | These methods should only be used by the producing side, i.e. the |
457 | code/module that eventually sends the signal. Note that it is also |
503 | code/module that eventually sends the signal. Note that it is also |
458 | the producer side which creates the condvar in most cases, but it isn't |
504 | the producer side which creates the condvar in most cases, but it isn't |
… | |
… | |
591 | =item $bool = $cv->ready |
637 | =item $bool = $cv->ready |
592 | |
638 | |
593 | Returns true when the condition is "true", i.e. whether C<send> or |
639 | Returns true when the condition is "true", i.e. whether C<send> or |
594 | C<croak> have been called. |
640 | C<croak> have been called. |
595 | |
641 | |
596 | =item $cb = $cv->cb ([new callback]) |
642 | =item $cb = $cv->cb ($cb->($cv)) |
597 | |
643 | |
598 | This is a mutator function that returns the callback set and optionally |
644 | This is a mutator function that returns the callback set and optionally |
599 | replaces it before doing so. |
645 | replaces it before doing so. |
600 | |
646 | |
601 | The callback will be called when the condition becomes "true", i.e. when |
647 | The callback will be called when the condition becomes "true", i.e. when |
… | |
… | |
738 | =item L<AnyEvent::Util> |
784 | =item L<AnyEvent::Util> |
739 | |
785 | |
740 | Contains various utility functions that replace often-used but blocking |
786 | Contains various utility functions that replace often-used but blocking |
741 | functions such as C<inet_aton> by event-/callback-based versions. |
787 | functions such as C<inet_aton> by event-/callback-based versions. |
742 | |
788 | |
743 | =item L<AnyEvent::Handle> |
|
|
744 | |
|
|
745 | Provide read and write buffers and manages watchers for reads and writes. |
|
|
746 | |
|
|
747 | =item L<AnyEvent::Socket> |
789 | =item L<AnyEvent::Socket> |
748 | |
790 | |
749 | Provides various utility functions for (internet protocol) sockets, |
791 | Provides various utility functions for (internet protocol) sockets, |
750 | addresses and name resolution. Also functions to create non-blocking tcp |
792 | addresses and name resolution. Also functions to create non-blocking tcp |
751 | connections or tcp servers, with IPv6 and SRV record support and more. |
793 | connections or tcp servers, with IPv6 and SRV record support and more. |
752 | |
794 | |
|
|
795 | =item L<AnyEvent::Handle> |
|
|
796 | |
|
|
797 | Provide read and write buffers, manages watchers for reads and writes, |
|
|
798 | supports raw and formatted I/O, I/O queued and fully transparent and |
|
|
799 | non-blocking SSL/TLS. |
|
|
800 | |
753 | =item L<AnyEvent::DNS> |
801 | =item L<AnyEvent::DNS> |
754 | |
802 | |
755 | Provides rich asynchronous DNS resolver capabilities. |
803 | Provides rich asynchronous DNS resolver capabilities. |
756 | |
804 | |
|
|
805 | =item L<AnyEvent::HTTP> |
|
|
806 | |
|
|
807 | A simple-to-use HTTP library that is capable of making a lot of concurrent |
|
|
808 | HTTP requests. |
|
|
809 | |
757 | =item L<AnyEvent::HTTPD> |
810 | =item L<AnyEvent::HTTPD> |
758 | |
811 | |
759 | Provides a simple web application server framework. |
812 | Provides a simple web application server framework. |
760 | |
813 | |
761 | =item L<AnyEvent::FastPing> |
814 | =item L<AnyEvent::FastPing> |
762 | |
815 | |
763 | The fastest ping in the west. |
816 | The fastest ping in the west. |
764 | |
817 | |
|
|
818 | =item L<AnyEvent::DBI> |
|
|
819 | |
|
|
820 | Executes L<DBI> requests asynchronously in a proxy process. |
|
|
821 | |
|
|
822 | =item L<AnyEvent::AIO> |
|
|
823 | |
|
|
824 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
825 | programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent |
|
|
826 | together. |
|
|
827 | |
|
|
828 | =item L<AnyEvent::BDB> |
|
|
829 | |
|
|
830 | Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses |
|
|
831 | L<BDB> and AnyEvent together. |
|
|
832 | |
|
|
833 | =item L<AnyEvent::GPSD> |
|
|
834 | |
|
|
835 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
|
|
836 | |
|
|
837 | =item L<AnyEvent::IGS> |
|
|
838 | |
|
|
839 | A non-blocking interface to the Internet Go Server protocol (used by |
|
|
840 | L<App::IGS>). |
|
|
841 | |
765 | =item L<Net::IRC3> |
842 | =item L<AnyEvent::IRC> |
766 | |
843 | |
767 | AnyEvent based IRC client module family. |
844 | AnyEvent based IRC client module family (replacing the older Net::IRC3). |
768 | |
845 | |
769 | =item L<Net::XMPP2> |
846 | =item L<Net::XMPP2> |
770 | |
847 | |
771 | AnyEvent based XMPP (Jabber protocol) module family. |
848 | AnyEvent based XMPP (Jabber protocol) module family. |
772 | |
849 | |
… | |
… | |
781 | |
858 | |
782 | =item L<Coro> |
859 | =item L<Coro> |
783 | |
860 | |
784 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
861 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
785 | |
862 | |
786 | =item L<AnyEvent::AIO>, L<IO::AIO> |
|
|
787 | |
|
|
788 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
789 | programmer. AnyEvent::AIO transparently fuses IO::AIO and AnyEvent |
|
|
790 | together. |
|
|
791 | |
|
|
792 | =item L<AnyEvent::BDB>, L<BDB> |
|
|
793 | |
|
|
794 | Truly asynchronous Berkeley DB access. AnyEvent::AIO transparently fuses |
|
|
795 | IO::AIO and AnyEvent together. |
|
|
796 | |
|
|
797 | =item L<IO::Lambda> |
863 | =item L<IO::Lambda> |
798 | |
864 | |
799 | The lambda approach to I/O - don't ask, look there. Can use AnyEvent. |
865 | The lambda approach to I/O - don't ask, look there. Can use AnyEvent. |
800 | |
866 | |
801 | =back |
867 | =back |
… | |
… | |
803 | =cut |
869 | =cut |
804 | |
870 | |
805 | package AnyEvent; |
871 | package AnyEvent; |
806 | |
872 | |
807 | no warnings; |
873 | no warnings; |
808 | use strict; |
874 | use strict qw(vars subs); |
809 | |
875 | |
810 | use Carp; |
876 | use Carp; |
811 | |
877 | |
812 | our $VERSION = 4.11; |
878 | our $VERSION = 4.35; |
813 | our $MODEL; |
879 | our $MODEL; |
814 | |
880 | |
815 | our $AUTOLOAD; |
881 | our $AUTOLOAD; |
816 | our @ISA; |
882 | our @ISA; |
817 | |
883 | |
… | |
… | |
920 | $MODEL |
986 | $MODEL |
921 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
987 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
922 | } |
988 | } |
923 | } |
989 | } |
924 | |
990 | |
|
|
991 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
|
|
992 | |
925 | unshift @ISA, $MODEL; |
993 | unshift @ISA, $MODEL; |
926 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
994 | |
|
|
995 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
927 | |
996 | |
928 | (shift @post_detect)->() while @post_detect; |
997 | (shift @post_detect)->() while @post_detect; |
929 | } |
998 | } |
930 | |
999 | |
931 | $MODEL |
1000 | $MODEL |
… | |
… | |
941 | |
1010 | |
942 | my $class = shift; |
1011 | my $class = shift; |
943 | $class->$func (@_); |
1012 | $class->$func (@_); |
944 | } |
1013 | } |
945 | |
1014 | |
|
|
1015 | # utility function to dup a filehandle. this is used by many backends |
|
|
1016 | # to support binding more than one watcher per filehandle (they usually |
|
|
1017 | # allow only one watcher per fd, so we dup it to get a different one). |
|
|
1018 | sub _dupfh($$$$) { |
|
|
1019 | my ($poll, $fh, $r, $w) = @_; |
|
|
1020 | |
|
|
1021 | # cygwin requires the fh mode to be matching, unix doesn't |
|
|
1022 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
|
|
1023 | : $poll eq "w" ? ($w, ">") |
|
|
1024 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
|
|
1025 | |
|
|
1026 | open my $fh2, "$mode&" . fileno $fh |
|
|
1027 | or die "cannot dup() filehandle: $!"; |
|
|
1028 | |
|
|
1029 | # we assume CLOEXEC is already set by perl in all important cases |
|
|
1030 | |
|
|
1031 | ($fh2, $rw) |
|
|
1032 | } |
|
|
1033 | |
946 | package AnyEvent::Base; |
1034 | package AnyEvent::Base; |
947 | |
1035 | |
948 | # default implementation for now and time |
1036 | # default implementation for now and time |
949 | |
1037 | |
950 | use Time::HiRes (); |
1038 | BEGIN { |
|
|
1039 | if (eval "use Time::HiRes (); time (); 1") { |
|
|
1040 | *_time = \&Time::HiRes::time; |
|
|
1041 | # if (eval "use POSIX (); (POSIX::times())... |
|
|
1042 | } else { |
|
|
1043 | *_time = sub { time }; # epic fail |
|
|
1044 | } |
|
|
1045 | } |
951 | |
1046 | |
952 | sub time { Time::HiRes::time } |
1047 | sub time { _time } |
953 | sub now { Time::HiRes::time } |
1048 | sub now { _time } |
954 | |
1049 | |
955 | # default implementation for ->condvar |
1050 | # default implementation for ->condvar |
956 | |
1051 | |
957 | sub condvar { |
1052 | sub condvar { |
958 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
1053 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
959 | } |
1054 | } |
960 | |
1055 | |
961 | # default implementation for ->signal |
1056 | # default implementation for ->signal |
962 | |
1057 | |
963 | our %SIG_CB; |
1058 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
|
|
1059 | |
|
|
1060 | sub _signal_exec { |
|
|
1061 | sysread $SIGPIPE_R, my $dummy, 4; |
|
|
1062 | |
|
|
1063 | while (%SIG_EV) { |
|
|
1064 | for (keys %SIG_EV) { |
|
|
1065 | delete $SIG_EV{$_}; |
|
|
1066 | $_->() for values %{ $SIG_CB{$_} || {} }; |
|
|
1067 | } |
|
|
1068 | } |
|
|
1069 | } |
964 | |
1070 | |
965 | sub signal { |
1071 | sub signal { |
966 | my (undef, %arg) = @_; |
1072 | my (undef, %arg) = @_; |
967 | |
1073 | |
|
|
1074 | unless ($SIGPIPE_R) { |
|
|
1075 | require Fcntl; |
|
|
1076 | |
|
|
1077 | if (AnyEvent::WIN32) { |
|
|
1078 | require AnyEvent::Util; |
|
|
1079 | |
|
|
1080 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
|
|
1081 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R; |
|
|
1082 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case |
|
|
1083 | } else { |
|
|
1084 | pipe $SIGPIPE_R, $SIGPIPE_W; |
|
|
1085 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
|
|
1086 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
|
|
1087 | } |
|
|
1088 | |
|
|
1089 | $SIGPIPE_R |
|
|
1090 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
|
|
1091 | |
|
|
1092 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1093 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1094 | |
|
|
1095 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
|
|
1096 | } |
|
|
1097 | |
968 | my $signal = uc $arg{signal} |
1098 | my $signal = uc $arg{signal} |
969 | or Carp::croak "required option 'signal' is missing"; |
1099 | or Carp::croak "required option 'signal' is missing"; |
970 | |
1100 | |
971 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1101 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
972 | $SIG{$signal} ||= sub { |
1102 | $SIG{$signal} ||= sub { |
973 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
1103 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
|
|
1104 | undef $SIG_EV{$signal}; |
974 | }; |
1105 | }; |
975 | |
1106 | |
976 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
1107 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
977 | } |
1108 | } |
978 | |
1109 | |
979 | sub AnyEvent::Base::Signal::DESTROY { |
1110 | sub AnyEvent::Base::Signal::DESTROY { |
980 | my ($signal, $cb) = @{$_[0]}; |
1111 | my ($signal, $cb) = @{$_[0]}; |
981 | |
1112 | |
982 | delete $SIG_CB{$signal}{$cb}; |
1113 | delete $SIG_CB{$signal}{$cb}; |
983 | |
1114 | |
984 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
1115 | delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
985 | } |
1116 | } |
986 | |
1117 | |
987 | # default implementation for ->child |
1118 | # default implementation for ->child |
988 | |
1119 | |
989 | our %PID_CB; |
1120 | our %PID_CB; |
… | |
… | |
1097 | |
1228 | |
1098 | # undocumented/compatibility with pre-3.4 |
1229 | # undocumented/compatibility with pre-3.4 |
1099 | *broadcast = \&send; |
1230 | *broadcast = \&send; |
1100 | *wait = \&_wait; |
1231 | *wait = \&_wait; |
1101 | |
1232 | |
|
|
1233 | =head1 ERROR AND EXCEPTION HANDLING |
|
|
1234 | |
|
|
1235 | In general, AnyEvent does not do any error handling - it relies on the |
|
|
1236 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
|
|
1237 | the C<PERL_ANYEVENT_STRICT> environment variable, below) provides strict |
|
|
1238 | checking of all AnyEvent methods, however, which is highly useful during |
|
|
1239 | development. |
|
|
1240 | |
|
|
1241 | As for exception handling (i.e. runtime errors and exceptions thrown while |
|
|
1242 | executing a callback), this is not only highly event-loop specific, but |
|
|
1243 | also not in any way wrapped by this module, as this is the job of the main |
|
|
1244 | program. |
|
|
1245 | |
|
|
1246 | The pure perl event loop simply re-throws the exception (usually |
|
|
1247 | within C<< condvar->recv >>), the L<Event> and L<EV> modules call C<< |
|
|
1248 | $Event/EV::DIED->() >>, L<Glib> uses C<< install_exception_handler >> and |
|
|
1249 | so on. |
|
|
1250 | |
|
|
1251 | =head1 ENVIRONMENT VARIABLES |
|
|
1252 | |
|
|
1253 | The following environment variables are used by this module or its |
|
|
1254 | submodules: |
|
|
1255 | |
|
|
1256 | =over 4 |
|
|
1257 | |
|
|
1258 | =item C<PERL_ANYEVENT_VERBOSE> |
|
|
1259 | |
|
|
1260 | By default, AnyEvent will be completely silent except in fatal |
|
|
1261 | conditions. You can set this environment variable to make AnyEvent more |
|
|
1262 | talkative. |
|
|
1263 | |
|
|
1264 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
|
|
1265 | conditions, such as not being able to load the event model specified by |
|
|
1266 | C<PERL_ANYEVENT_MODEL>. |
|
|
1267 | |
|
|
1268 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
|
|
1269 | model it chooses. |
|
|
1270 | |
|
|
1271 | =item C<PERL_ANYEVENT_STRICT> |
|
|
1272 | |
|
|
1273 | AnyEvent does not do much argument checking by default, as thorough |
|
|
1274 | argument checking is very costly. Setting this variable to a true value |
|
|
1275 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
|
|
1276 | check the arguments passed to most method calls. If it finds any problems |
|
|
1277 | it will croak. |
|
|
1278 | |
|
|
1279 | In other words, enables "strict" mode. |
|
|
1280 | |
|
|
1281 | Unlike C<use strict>, it is definitely recommended ot keep it off in |
|
|
1282 | production. Keeping C<PERL_ANYEVENT_STRICT=1> in your environment while |
|
|
1283 | developing programs can be very useful, however. |
|
|
1284 | |
|
|
1285 | =item C<PERL_ANYEVENT_MODEL> |
|
|
1286 | |
|
|
1287 | This can be used to specify the event model to be used by AnyEvent, before |
|
|
1288 | auto detection and -probing kicks in. It must be a string consisting |
|
|
1289 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
|
|
1290 | and the resulting module name is loaded and if the load was successful, |
|
|
1291 | used as event model. If it fails to load AnyEvent will proceed with |
|
|
1292 | auto detection and -probing. |
|
|
1293 | |
|
|
1294 | This functionality might change in future versions. |
|
|
1295 | |
|
|
1296 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
|
|
1297 | could start your program like this: |
|
|
1298 | |
|
|
1299 | PERL_ANYEVENT_MODEL=Perl perl ... |
|
|
1300 | |
|
|
1301 | =item C<PERL_ANYEVENT_PROTOCOLS> |
|
|
1302 | |
|
|
1303 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
|
|
1304 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
|
|
1305 | of auto probing). |
|
|
1306 | |
|
|
1307 | Must be set to a comma-separated list of protocols or address families, |
|
|
1308 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
|
|
1309 | used, and preference will be given to protocols mentioned earlier in the |
|
|
1310 | list. |
|
|
1311 | |
|
|
1312 | This variable can effectively be used for denial-of-service attacks |
|
|
1313 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1314 | small, as the program has to handle conenction and other failures anyways. |
|
|
1315 | |
|
|
1316 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1317 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1318 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1319 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1320 | IPv6, but prefer IPv6 over IPv4. |
|
|
1321 | |
|
|
1322 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1323 | |
|
|
1324 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1325 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1326 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1327 | default. |
|
|
1328 | |
|
|
1329 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1330 | EDNS0 in its DNS requests. |
|
|
1331 | |
|
|
1332 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1333 | |
|
|
1334 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1335 | will create in parallel. |
|
|
1336 | |
|
|
1337 | =back |
|
|
1338 | |
1102 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1339 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1103 | |
1340 | |
1104 | This is an advanced topic that you do not normally need to use AnyEvent in |
1341 | This is an advanced topic that you do not normally need to use AnyEvent in |
1105 | a module. This section is only of use to event loop authors who want to |
1342 | a module. This section is only of use to event loop authors who want to |
1106 | provide AnyEvent compatibility. |
1343 | provide AnyEvent compatibility. |
… | |
… | |
1139 | |
1376 | |
1140 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
1377 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
1141 | condition variables: code blocking while waiting for a condition will |
1378 | condition variables: code blocking while waiting for a condition will |
1142 | C<die>. This still works with most modules/usages, and blocking calls must |
1379 | C<die>. This still works with most modules/usages, and blocking calls must |
1143 | not be done in an interactive application, so it makes sense. |
1380 | not be done in an interactive application, so it makes sense. |
1144 | |
|
|
1145 | =head1 ENVIRONMENT VARIABLES |
|
|
1146 | |
|
|
1147 | The following environment variables are used by this module: |
|
|
1148 | |
|
|
1149 | =over 4 |
|
|
1150 | |
|
|
1151 | =item C<PERL_ANYEVENT_VERBOSE> |
|
|
1152 | |
|
|
1153 | By default, AnyEvent will be completely silent except in fatal |
|
|
1154 | conditions. You can set this environment variable to make AnyEvent more |
|
|
1155 | talkative. |
|
|
1156 | |
|
|
1157 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
|
|
1158 | conditions, such as not being able to load the event model specified by |
|
|
1159 | C<PERL_ANYEVENT_MODEL>. |
|
|
1160 | |
|
|
1161 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
|
|
1162 | model it chooses. |
|
|
1163 | |
|
|
1164 | =item C<PERL_ANYEVENT_MODEL> |
|
|
1165 | |
|
|
1166 | This can be used to specify the event model to be used by AnyEvent, before |
|
|
1167 | auto detection and -probing kicks in. It must be a string consisting |
|
|
1168 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
|
|
1169 | and the resulting module name is loaded and if the load was successful, |
|
|
1170 | used as event model. If it fails to load AnyEvent will proceed with |
|
|
1171 | auto detection and -probing. |
|
|
1172 | |
|
|
1173 | This functionality might change in future versions. |
|
|
1174 | |
|
|
1175 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
|
|
1176 | could start your program like this: |
|
|
1177 | |
|
|
1178 | PERL_ANYEVENT_MODEL=Perl perl ... |
|
|
1179 | |
|
|
1180 | =item C<PERL_ANYEVENT_PROTOCOLS> |
|
|
1181 | |
|
|
1182 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
|
|
1183 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
|
|
1184 | of auto probing). |
|
|
1185 | |
|
|
1186 | Must be set to a comma-separated list of protocols or address families, |
|
|
1187 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
|
|
1188 | used, and preference will be given to protocols mentioned earlier in the |
|
|
1189 | list. |
|
|
1190 | |
|
|
1191 | This variable can effectively be used for denial-of-service attacks |
|
|
1192 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1193 | small, as the program has to handle connection errors already- |
|
|
1194 | |
|
|
1195 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1196 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1197 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1198 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1199 | IPv6, but prefer IPv6 over IPv4. |
|
|
1200 | |
|
|
1201 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1202 | |
|
|
1203 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1204 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1205 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1206 | default. |
|
|
1207 | |
|
|
1208 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1209 | EDNS0 in its DNS requests. |
|
|
1210 | |
|
|
1211 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1212 | |
|
|
1213 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1214 | will create in parallel. |
|
|
1215 | |
|
|
1216 | =back |
|
|
1217 | |
1381 | |
1218 | =head1 EXAMPLE PROGRAM |
1382 | =head1 EXAMPLE PROGRAM |
1219 | |
1383 | |
1220 | The following program uses an I/O watcher to read data from STDIN, a timer |
1384 | The following program uses an I/O watcher to read data from STDIN, a timer |
1221 | to display a message once per second, and a condition variable to quit the |
1385 | to display a message once per second, and a condition variable to quit the |
… | |
… | |
1415 | watcher. |
1579 | watcher. |
1416 | |
1580 | |
1417 | =head3 Results |
1581 | =head3 Results |
1418 | |
1582 | |
1419 | name watchers bytes create invoke destroy comment |
1583 | name watchers bytes create invoke destroy comment |
1420 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
1584 | EV/EV 400000 224 0.47 0.35 0.27 EV native interface |
1421 | EV/Any 100000 244 2.50 0.46 0.29 EV + AnyEvent watchers |
1585 | EV/Any 100000 224 2.88 0.34 0.27 EV + AnyEvent watchers |
1422 | CoroEV/Any 100000 244 2.49 0.44 0.29 coroutines + Coro::Signal |
1586 | CoroEV/Any 100000 224 2.85 0.35 0.28 coroutines + Coro::Signal |
1423 | Perl/Any 100000 513 4.92 0.87 1.12 pure perl implementation |
1587 | Perl/Any 100000 452 4.13 0.73 0.95 pure perl implementation |
1424 | Event/Event 16000 516 31.88 31.30 0.85 Event native interface |
1588 | Event/Event 16000 517 32.20 31.80 0.81 Event native interface |
1425 | Event/Any 16000 590 35.75 31.42 1.08 Event + AnyEvent watchers |
1589 | Event/Any 16000 590 35.85 31.55 1.06 Event + AnyEvent watchers |
1426 | Glib/Any 16000 1357 98.22 12.41 54.00 quadratic behaviour |
1590 | Glib/Any 16000 1357 102.33 12.31 51.00 quadratic behaviour |
1427 | Tk/Any 2000 1860 26.97 67.98 14.00 SEGV with >> 2000 watchers |
1591 | Tk/Any 2000 1860 27.20 66.31 14.00 SEGV with >> 2000 watchers |
1428 | POE/Event 2000 6644 108.64 736.02 14.73 via POE::Loop::Event |
1592 | POE/Event 2000 6328 109.99 751.67 14.02 via POE::Loop::Event |
1429 | POE/Select 2000 6343 94.13 809.12 565.96 via POE::Loop::Select |
1593 | POE/Select 2000 6027 94.54 809.13 579.80 via POE::Loop::Select |
1430 | |
1594 | |
1431 | =head3 Discussion |
1595 | =head3 Discussion |
1432 | |
1596 | |
1433 | The benchmark does I<not> measure scalability of the event loop very |
1597 | The benchmark does I<not> measure scalability of the event loop very |
1434 | well. For example, a select-based event loop (such as the pure perl one) |
1598 | well. For example, a select-based event loop (such as the pure perl one) |
… | |
… | |
1636 | watchers, as the management overhead dominates. |
1800 | watchers, as the management overhead dominates. |
1637 | |
1801 | |
1638 | =back |
1802 | =back |
1639 | |
1803 | |
1640 | |
1804 | |
|
|
1805 | =head1 SIGNALS |
|
|
1806 | |
|
|
1807 | AnyEvent currently installs handlers for these signals: |
|
|
1808 | |
|
|
1809 | =over 4 |
|
|
1810 | |
|
|
1811 | =item SIGCHLD |
|
|
1812 | |
|
|
1813 | A handler for C<SIGCHLD> is installed by AnyEvent's child watcher |
|
|
1814 | emulation for event loops that do not support them natively. Also, some |
|
|
1815 | event loops install a similar handler. |
|
|
1816 | |
|
|
1817 | =item SIGPIPE |
|
|
1818 | |
|
|
1819 | A no-op handler is installed for C<SIGPIPE> when C<$SIG{PIPE}> is C<undef> |
|
|
1820 | when AnyEvent gets loaded. |
|
|
1821 | |
|
|
1822 | The rationale for this is that AnyEvent users usually do not really depend |
|
|
1823 | on SIGPIPE delivery (which is purely an optimisation for shell use, or |
|
|
1824 | badly-written programs), but C<SIGPIPE> can cause spurious and rare |
|
|
1825 | program exits as a lot of people do not expect C<SIGPIPE> when writing to |
|
|
1826 | some random socket. |
|
|
1827 | |
|
|
1828 | The rationale for installing a no-op handler as opposed to ignoring it is |
|
|
1829 | that this way, the handler will be restored to defaults on exec. |
|
|
1830 | |
|
|
1831 | Feel free to install your own handler, or reset it to defaults. |
|
|
1832 | |
|
|
1833 | =back |
|
|
1834 | |
|
|
1835 | =cut |
|
|
1836 | |
|
|
1837 | $SIG{PIPE} = sub { } |
|
|
1838 | unless defined $SIG{PIPE}; |
|
|
1839 | |
|
|
1840 | |
1641 | =head1 FORK |
1841 | =head1 FORK |
1642 | |
1842 | |
1643 | Most event libraries are not fork-safe. The ones who are usually are |
1843 | Most event libraries are not fork-safe. The ones who are usually are |
1644 | because they rely on inefficient but fork-safe C<select> or C<poll> |
1844 | because they rely on inefficient but fork-safe C<select> or C<poll> |
1645 | calls. Only L<EV> is fully fork-aware. |
1845 | calls. Only L<EV> is fully fork-aware. |
… | |
… | |
1658 | specified in the variable. |
1858 | specified in the variable. |
1659 | |
1859 | |
1660 | You can make AnyEvent completely ignore this variable by deleting it |
1860 | You can make AnyEvent completely ignore this variable by deleting it |
1661 | before the first watcher gets created, e.g. with a C<BEGIN> block: |
1861 | before the first watcher gets created, e.g. with a C<BEGIN> block: |
1662 | |
1862 | |
1663 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
1863 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
1664 | |
1864 | |
1665 | use AnyEvent; |
1865 | use AnyEvent; |
1666 | |
1866 | |
1667 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1867 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1668 | be used to probe what backend is used and gain other information (which is |
1868 | be used to probe what backend is used and gain other information (which is |
1669 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL). |
1869 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL), and |
|
|
1870 | $ENV{PERL_ANYEGENT_STRICT}. |
|
|
1871 | |
|
|
1872 | |
|
|
1873 | =head1 BUGS |
|
|
1874 | |
|
|
1875 | Perl 5.8 has numerous memleaks that sometimes hit this module and are hard |
|
|
1876 | to work around. If you suffer from memleaks, first upgrade to Perl 5.10 |
|
|
1877 | and check wether the leaks still show up. (Perl 5.10.0 has other annoying |
|
|
1878 | memleaks, such as leaking on C<map> and C<grep> but it is usually not as |
|
|
1879 | pronounced). |
1670 | |
1880 | |
1671 | |
1881 | |
1672 | =head1 SEE ALSO |
1882 | =head1 SEE ALSO |
1673 | |
1883 | |
1674 | Utility functions: L<AnyEvent::Util>. |
1884 | Utility functions: L<AnyEvent::Util>. |
… | |
… | |
1691 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. |
1901 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. |
1692 | |
1902 | |
1693 | |
1903 | |
1694 | =head1 AUTHOR |
1904 | =head1 AUTHOR |
1695 | |
1905 | |
1696 | Marc Lehmann <schmorp@schmorp.de> |
1906 | Marc Lehmann <schmorp@schmorp.de> |
1697 | http://home.schmorp.de/ |
1907 | http://home.schmorp.de/ |
1698 | |
1908 | |
1699 | =cut |
1909 | =cut |
1700 | |
1910 | |
1701 | 1 |
1911 | 1 |
1702 | |
1912 | |