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, Coro::EV, Coro::Event, Glib, Tk, Perl, Event::Lib - various supported event loops |
5 | EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl, Event::Lib, Qt - various supported event loops |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
… | |
… | |
78 | |
78 | |
79 | The interface itself is vaguely similar, but not identical to the L<Event> |
79 | The interface itself is vaguely similar, but not identical to the L<Event> |
80 | module. |
80 | module. |
81 | |
81 | |
82 | During the first call of any watcher-creation method, the module tries |
82 | During the first call of any watcher-creation method, the module tries |
83 | to detect the currently loaded event loop by probing whether one of the |
83 | to detect the currently loaded event loop by probing whether one of |
84 | following modules is already loaded: L<Coro::EV>, L<Coro::Event>, L<EV>, |
84 | the following modules is already loaded: L<Coro::EV>, L<Coro::Event>, |
85 | L<Event>, L<Glib>, L<Tk>. The first one found is used. If none are found, |
85 | L<EV>, L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>. The first one |
86 | the module tries to load these modules in the stated order. The first one |
86 | found is used. If none are found, the module tries to load these modules |
|
|
87 | (excluding Event::Lib and Qt) in the order given. The first one that can |
87 | that can be successfully loaded will be used. If, after this, still none |
88 | be successfully loaded will be used. If, after this, still none could be |
88 | could be found, AnyEvent will fall back to a pure-perl event loop, which |
89 | found, AnyEvent will fall back to a pure-perl event loop, which is not |
89 | is not very efficient, but should work everywhere. |
90 | very efficient, but should work everywhere. |
90 | |
91 | |
91 | Because AnyEvent first checks for modules that are already loaded, loading |
92 | Because AnyEvent first checks for modules that are already loaded, loading |
92 | an event model explicitly before first using AnyEvent will likely make |
93 | an event model explicitly before first using AnyEvent will likely make |
93 | that model the default. For example: |
94 | that model the default. For example: |
94 | |
95 | |
… | |
… | |
145 | events. C<poll> must be a string that is either C<r> or C<w>, which |
146 | events. C<poll> must be a string that is either C<r> or C<w>, which |
146 | creates a watcher waiting for "r"eadable or "w"ritable events, |
147 | creates a watcher waiting for "r"eadable or "w"ritable events, |
147 | respectively. C<cb> is the callback to invoke each time the file handle |
148 | respectively. C<cb> is the callback to invoke each time the file handle |
148 | becomes ready. |
149 | becomes ready. |
149 | |
150 | |
150 | File handles will be kept alive, so as long as the watcher exists, the |
151 | As long as the I/O watcher exists it will keep the file descriptor or a |
151 | file handle exists, too. |
152 | copy of it alive/open. |
152 | |
153 | |
153 | It is not allowed to close a file handle as long as any watcher is active |
154 | It is not allowed to close a file handle as long as any watcher is active |
154 | on the underlying file descriptor. |
155 | on the underlying file descriptor. |
155 | |
156 | |
156 | Some event loops issue spurious readyness notifications, so you should |
157 | Some event loops issue spurious readyness notifications, so you should |
… | |
… | |
206 | |
207 | |
207 | There are two ways to handle timers: based on real time (relative, "fire |
208 | There are two ways to handle timers: based on real time (relative, "fire |
208 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
209 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
209 | o'clock"). |
210 | o'clock"). |
210 | |
211 | |
211 | While most event loops expect timers to specified in a relative way, they use |
212 | While most event loops expect timers to specified in a relative way, they |
212 | absolute time internally. This makes a difference when your clock "jumps", |
213 | use absolute time internally. This makes a difference when your clock |
213 | for example, when ntp decides to set your clock backwards from the wrong 2014-01-01 to |
214 | "jumps", for example, when ntp decides to set your clock backwards from |
214 | 2008-01-01, a watcher that you created to fire "after" a second might actually take |
215 | the wrong date of 2014-01-01 to 2008-01-01, a watcher that is supposed to |
215 | six years to finally fire. |
216 | fire "after" a second might actually take six years to finally fire. |
216 | |
217 | |
217 | AnyEvent cannot compensate for this. The only event loop that is conscious |
218 | AnyEvent cannot compensate for this. The only event loop that is conscious |
218 | about these issues is L<EV>, which offers both relative (ev_timer) and |
219 | about these issues is L<EV>, which offers both relative (ev_timer, based |
219 | absolute (ev_periodic) timers. |
220 | on true relative time) and absolute (ev_periodic, based on wallclock time) |
|
|
221 | timers. |
220 | |
222 | |
221 | AnyEvent always prefers relative timers, if available, matching the |
223 | AnyEvent always prefers relative timers, if available, matching the |
222 | AnyEvent API. |
224 | AnyEvent API. |
223 | |
225 | |
224 | =head2 SIGNAL WATCHERS |
226 | =head2 SIGNAL WATCHERS |
225 | |
227 | |
226 | You can watch for signals using a signal watcher, C<signal> is the signal |
228 | You can watch for signals using a signal watcher, C<signal> is the signal |
227 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
229 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
228 | be invoked whenever a signal occurs. |
230 | be invoked whenever a signal occurs. |
229 | |
231 | |
230 | Multiple signals occurances can be clumped together into one callback |
232 | Multiple signal occurances can be clumped together into one callback |
231 | invocation, and callback invocation will be synchronous. synchronous means |
233 | invocation, and callback invocation will be synchronous. synchronous means |
232 | that it might take a while until the signal gets handled by the process, |
234 | that it might take a while until the signal gets handled by the process, |
233 | but it is guarenteed not to interrupt any other callbacks. |
235 | but it is guarenteed not to interrupt any other callbacks. |
234 | |
236 | |
235 | The main advantage of using these watchers is that you can share a signal |
237 | The main advantage of using these watchers is that you can share a signal |
… | |
… | |
353 | |
355 | |
354 | The known classes so far are: |
356 | The known classes so far are: |
355 | |
357 | |
356 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
358 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
357 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
359 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
358 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
360 | AnyEvent::Impl::EV based on EV (an interface to libev, best choice). |
359 | AnyEvent::Impl::Event based on Event, also second best choice :) |
361 | AnyEvent::Impl::Event based on Event, second best choice. |
360 | AnyEvent::Impl::Glib based on Glib, third-best choice. |
362 | AnyEvent::Impl::Glib based on Glib, third-best choice. |
361 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
363 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
362 | AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable. |
364 | AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable. |
|
|
365 | AnyEvent::Impl::Qt based on Qt, cannot be autoprobed (see its docs). |
363 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
366 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
364 | |
367 | |
365 | =item AnyEvent::detect |
368 | =item AnyEvent::detect |
366 | |
369 | |
367 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
370 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
… | |
… | |
419 | no warnings; |
422 | no warnings; |
420 | use strict; |
423 | use strict; |
421 | |
424 | |
422 | use Carp; |
425 | use Carp; |
423 | |
426 | |
424 | our $VERSION = '3.12'; |
427 | our $VERSION = '3.2'; |
425 | our $MODEL; |
428 | our $MODEL; |
426 | |
429 | |
427 | our $AUTOLOAD; |
430 | our $AUTOLOAD; |
428 | our @ISA; |
431 | our @ISA; |
429 | |
432 | |
… | |
… | |
437 | [EV:: => AnyEvent::Impl::EV::], |
440 | [EV:: => AnyEvent::Impl::EV::], |
438 | [Event:: => AnyEvent::Impl::Event::], |
441 | [Event:: => AnyEvent::Impl::Event::], |
439 | [Glib:: => AnyEvent::Impl::Glib::], |
442 | [Glib:: => AnyEvent::Impl::Glib::], |
440 | [Tk:: => AnyEvent::Impl::Tk::], |
443 | [Tk:: => AnyEvent::Impl::Tk::], |
441 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
444 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
442 | [Event::Lib:: => AnyEvent::Impl::EventLib::], |
|
|
443 | ); |
445 | ); |
|
|
446 | my @models_detect = ( |
|
|
447 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
|
|
448 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
|
|
449 | ); |
444 | |
450 | |
445 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); |
451 | our %method = map +($_ => 1), qw(io timer signal child condvar broadcast wait one_event DESTROY); |
446 | |
452 | |
447 | sub detect() { |
453 | sub detect() { |
448 | unless ($MODEL) { |
454 | unless ($MODEL) { |
449 | no strict 'refs'; |
455 | no strict 'refs'; |
450 | |
456 | |
… | |
… | |
456 | } |
462 | } |
457 | } |
463 | } |
458 | |
464 | |
459 | # check for already loaded models |
465 | # check for already loaded models |
460 | unless ($MODEL) { |
466 | unless ($MODEL) { |
461 | for (@REGISTRY, @models) { |
467 | for (@REGISTRY, @models, @models_detect) { |
462 | my ($package, $model) = @$_; |
468 | my ($package, $model) = @$_; |
463 | if (${"$package\::VERSION"} > 0) { |
469 | if (${"$package\::VERSION"} > 0) { |
464 | if (eval "require $model") { |
470 | if (eval "require $model") { |
465 | $MODEL = $model; |
471 | $MODEL = $model; |
466 | warn "AnyEvent: autodetected model '$model', using it.\n" if $verbose > 1; |
472 | warn "AnyEvent: autodetected model '$model', using it.\n" if $verbose > 1; |
… | |
… | |
858 | |
864 | |
859 | =head1 SEE ALSO |
865 | =head1 SEE ALSO |
860 | |
866 | |
861 | Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
867 | Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
862 | L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>, |
868 | L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>, |
863 | L<Event::Lib>. |
869 | L<Event::Lib>, L<Qt>. |
864 | |
870 | |
865 | Implementations: L<AnyEvent::Impl::CoroEV>, L<AnyEvent::Impl::EV>, |
871 | Implementations: L<AnyEvent::Impl::CoroEV>, L<AnyEvent::Impl::EV>, |
866 | L<AnyEvent::Impl::CoroEvent>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>, |
872 | L<AnyEvent::Impl::CoroEvent>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>, |
867 | L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, L<AnyEvent::Impl::EventLib>. |
873 | L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, L<AnyEvent::Impl::EventLib>, |
|
|
874 | L<AnyEvent::Impl::Qt>. |
868 | |
875 | |
869 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
876 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
870 | |
877 | |
871 | =head1 AUTHOR |
878 | =head1 AUTHOR |
872 | |
879 | |