ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.54 by root, Tue Apr 22 05:12:19 2008 UTC vs.
Revision 1.59 by root, Thu Apr 24 08:38:13 2008 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent - provide framework for multiple event loops 3AnyEvent - provide framework for multiple event loops
4 4
5EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl - various supported event loops 5EV, 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
79The interface itself is vaguely similar, but not identical to the L<Event> 79The interface itself is vaguely similar, but not identical to the L<Event>
80module. 80module.
81 81
82During the first call of any watcher-creation method, the module tries 82During the first call of any watcher-creation method, the module tries
83to detect the currently loaded event loop by probing whether one of the 83to detect the currently loaded event loop by probing whether one of
84following modules is already loaded: L<Coro::EV>, L<Coro::Event>, L<EV>, 84the following modules is already loaded: L<Coro::EV>, L<Coro::Event>,
85L<Event>, L<Glib>, L<Tk>. The first one found is used. If none are found, 85L<EV>, L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>. The first one
86the module tries to load these modules in the stated order. The first one 86found 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
87that can be successfully loaded will be used. If, after this, still none 88be successfully loaded will be used. If, after this, still none could be
88could be found, AnyEvent will fall back to a pure-perl event loop, which 89found, AnyEvent will fall back to a pure-perl event loop, which is not
89is not very efficient, but should work everywhere. 90very efficient, but should work everywhere.
90 91
91Because AnyEvent first checks for modules that are already loaded, loading 92Because AnyEvent first checks for modules that are already loaded, loading
92an event model explicitly before first using AnyEvent will likely make 93an event model explicitly before first using AnyEvent will likely make
93that model the default. For example: 94that model the default. For example:
94 95
145events. C<poll> must be a string that is either C<r> or C<w>, which 146events. C<poll> must be a string that is either C<r> or C<w>, which
146creates a watcher waiting for "r"eadable or "w"ritable events, 147creates a watcher waiting for "r"eadable or "w"ritable events,
147respectively. C<cb> is the callback to invoke each time the file handle 148respectively. C<cb> is the callback to invoke each time the file handle
148becomes ready. 149becomes ready.
149 150
150File handles will be kept alive, so as long as the watcher exists, the 151As long as the I/O watcher exists it will keep the file descriptor or a
151file handle exists, too. 152copy of it alive/open.
152 153
153It is not allowed to close a file handle as long as any watcher is active 154It is not allowed to close a file handle as long as any watcher is active
154on the underlying file descriptor. 155on the underlying file descriptor.
155 156
156Some event loops issue spurious readyness notifications, so you should 157Some event loops issue spurious readyness notifications, so you should
206 207
207There are two ways to handle timers: based on real time (relative, "fire 208There are two ways to handle timers: based on real time (relative, "fire
208in 10 seconds") and based on wallclock time (absolute, "fire at 12 209in 10 seconds") and based on wallclock time (absolute, "fire at 12
209o'clock"). 210o'clock").
210 211
211While most event loops expect timers to specified in a relative way, they use 212While most event loops expect timers to specified in a relative way, they
212absolute time internally. This makes a difference when your clock "jumps", 213use absolute time internally. This makes a difference when your clock
213for 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
2142008-01-01, a watcher that you created to fire "after" a second might actually take 215the wrong date of 2014-01-01 to 2008-01-01, a watcher that is supposed to
215six years to finally fire. 216fire "after" a second might actually take six years to finally fire.
216 217
217AnyEvent cannot compensate for this. The only event loop that is conscious 218AnyEvent cannot compensate for this. The only event loop that is conscious
218about these issues is L<EV>, which offers both relative (ev_timer) and 219about these issues is L<EV>, which offers both relative (ev_timer, based
219absolute (ev_periodic) timers. 220on true relative time) and absolute (ev_periodic, based on wallclock time)
221timers.
220 222
221AnyEvent always prefers relative timers, if available, matching the 223AnyEvent always prefers relative timers, if available, matching the
222AnyEvent API. 224AnyEvent API.
223 225
224=head2 SIGNAL WATCHERS 226=head2 SIGNAL WATCHERS
225 227
226You can watch for signals using a signal watcher, C<signal> is the signal 228You can watch for signals using a signal watcher, C<signal> is the signal
227I<name> without any C<SIG> prefix, C<cb> is the Perl callback to 229I<name> without any C<SIG> prefix, C<cb> is the Perl callback to
228be invoked whenever a signal occurs. 230be invoked whenever a signal occurs.
229 231
230Multiple signals occurances can be clumped together into one callback 232Multiple signal occurances can be clumped together into one callback
231invocation, and callback invocation will be synchronous. synchronous means 233invocation, and callback invocation will be synchronous. synchronous means
232that it might take a while until the signal gets handled by the process, 234that it might take a while until the signal gets handled by the process,
233but it is guarenteed not to interrupt any other callbacks. 235but it is guarenteed not to interrupt any other callbacks.
234 236
235The main advantage of using these watchers is that you can share a signal 237The main advantage of using these watchers is that you can share a signal
353 355
354The known classes so far are: 356The 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).
366 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
363 367
364=item AnyEvent::detect 368=item AnyEvent::detect
365 369
366Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model 370Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model
367if necessary. You should only call this function right before you would 371if necessary. You should only call this function right before you would
418no warnings; 422no warnings;
419use strict; 423use strict;
420 424
421use Carp; 425use Carp;
422 426
423our $VERSION = '3.12'; 427our $VERSION = '3.2';
424our $MODEL; 428our $MODEL;
425 429
426our $AUTOLOAD; 430our $AUTOLOAD;
427our @ISA; 431our @ISA;
428 432
437 [Event:: => AnyEvent::Impl::Event::], 441 [Event:: => AnyEvent::Impl::Event::],
438 [Glib:: => AnyEvent::Impl::Glib::], 442 [Glib:: => AnyEvent::Impl::Glib::],
439 [Tk:: => AnyEvent::Impl::Tk::], 443 [Tk:: => AnyEvent::Impl::Tk::],
440 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 444 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
441); 445);
446my @models_detect = (
447 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program
448 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy
449);
442 450
443our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); 451our %method = map +($_ => 1), qw(io timer signal child condvar broadcast wait one_event DESTROY);
444 452
445sub detect() { 453sub detect() {
446 unless ($MODEL) { 454 unless ($MODEL) {
447 no strict 'refs'; 455 no strict 'refs';
448 456
457 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
458 my $model = "AnyEvent::Impl::$1";
459 if (eval "require $model") {
460 $MODEL = $model;
461 warn "AnyEvent: loaded model '$model' (forced by \$PERL_ANYEVENT_MODEL), using it.\n" if $verbose > 1;
462 }
463 }
464
449 # check for already loaded models 465 # check for already loaded models
466 unless ($MODEL) {
450 for (@REGISTRY, @models) { 467 for (@REGISTRY, @models, @models_detect) {
451 my ($package, $model) = @$_; 468 my ($package, $model) = @$_;
452 if (${"$package\::VERSION"} > 0) { 469 if (${"$package\::VERSION"} > 0) {
453 if (eval "require $model") { 470 if (eval "require $model") {
454 $MODEL = $model; 471 $MODEL = $model;
455 warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; 472 warn "AnyEvent: autodetected model '$model', using it.\n" if $verbose > 1;
456 last; 473 last;
474 }
457 } 475 }
458 } 476 }
459 }
460 477
461 unless ($MODEL) { 478 unless ($MODEL) {
462 # try to load a model 479 # try to load a model
463 480
464 for (@REGISTRY, @models) { 481 for (@REGISTRY, @models) {
465 my ($package, $model) = @$_; 482 my ($package, $model) = @$_;
466 if (eval "require $package" 483 if (eval "require $package"
467 and ${"$package\::VERSION"} > 0 484 and ${"$package\::VERSION"} > 0
468 and eval "require $model") { 485 and eval "require $model") {
469 $MODEL = $model; 486 $MODEL = $model;
470 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; 487 warn "AnyEvent: autoprobed model '$model', using it.\n" if $verbose > 1;
471 last; 488 last;
489 }
472 } 490 }
491
492 $MODEL
493 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event) or Glib.";
473 } 494 }
474
475 $MODEL
476 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event), Glib or Tk.";
477 } 495 }
478 496
479 unshift @ISA, $MODEL; 497 unshift @ISA, $MODEL;
480 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 498 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
481 } 499 }
637 655
638=head1 ENVIRONMENT VARIABLES 656=head1 ENVIRONMENT VARIABLES
639 657
640The following environment variables are used by this module: 658The following environment variables are used by this module:
641 659
642C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, cause AnyEvent to 660=over 4
643report to STDERR which event model it chooses. 661
662=item C<PERL_ANYEVENT_VERBOSE>
663
664When set to C<2> or higher, cause AnyEvent to report to STDERR which event
665model it chooses.
666
667=item C<PERL_ANYEVENT_MODEL>
668
669This can be used to specify the event model to be used by AnyEvent, before
670autodetection and -probing kicks in. It must be a string consisting
671entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended
672and the resulting module name is loaded and if the load was successful,
673used as event model. If it fails to load AnyEvent will proceed with
674autodetection and -probing.
675
676This functionality might change in future versions.
677
678For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you
679could start your program like this:
680
681 PERL_ANYEVENT_MODEL=Perl perl ...
682
683=back
644 684
645=head1 EXAMPLE PROGRAM 685=head1 EXAMPLE PROGRAM
646 686
647The following program uses an IO watcher to read data from STDIN, a timer 687The following program uses an IO watcher to read data from STDIN, a timer
648to display a message once per second, and a condition variable to quit the 688to display a message once per second, and a condition variable to quit the
796 $quit->broadcast; 836 $quit->broadcast;
797 }); 837 });
798 838
799 $quit->wait; 839 $quit->wait;
800 840
841=head1 FORK
842
843Most event libraries are not fork-safe. The ones who are usually are
844because they are so inefficient. Only L<EV> is fully fork-aware.
845
846If you have to fork, you must either do so I<before> creating your first
847watcher OR you must not use AnyEvent at all in the child.
848
849=head1 SECURITY CONSIDERATIONS
850
851AnyEvent can be forced to load any event model via
852$ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used to
853execute arbitrary code or directly gain access, it can easily be used to
854make the program hang or malfunction in subtle ways, as AnyEvent watchers
855will not be active when the program uses a different event model than
856specified in the variable.
857
858You can make AnyEvent completely ignore this variable by deleting it
859before the first watcher gets created, e.g. with a C<BEGIN> block:
860
861 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
862
863 use AnyEvent;
864
801=head1 SEE ALSO 865=head1 SEE ALSO
802 866
803Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>, 867Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>,
804L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>. 868L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>,
869L<Event::Lib>, L<Qt>.
805 870
806Implementations: L<AnyEvent::Impl::CoroEV>, L<AnyEvent::Impl::EV>, 871Implementations: L<AnyEvent::Impl::CoroEV>, L<AnyEvent::Impl::EV>,
807L<AnyEvent::Impl::CoroEvent>, L<AnyEvent::Impl::Event>, 872L<AnyEvent::Impl::CoroEvent>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>,
808L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>. 873L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, L<AnyEvent::Impl::EventLib>,
874L<AnyEvent::Impl::Qt>.
809 875
810Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. 876Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>.
811 877
812=head1 AUTHOR 878=head1 AUTHOR
813 879

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines