--- AnyEvent/lib/AnyEvent.pm 2008/05/30 21:43:26 1.147 +++ AnyEvent/lib/AnyEvent.pm 2008/08/20 12:37:21 1.176 @@ -1,4 +1,4 @@ -=head1 => NAME +=head1 NAME AnyEvent - provide framework for multiple event loops @@ -8,17 +8,32 @@ use AnyEvent; - my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { - ... - }); + my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... }); + + my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); + my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... + + print AnyEvent->now; # prints current event loop time + print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. + + my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); - my $w = AnyEvent->timer (after => $seconds, cb => sub { + my $w = AnyEvent->child (pid => $pid, cb => sub { + my ($pid, $status) = @_; ... }); my $w = AnyEvent->condvar; # stores whether a condition was flagged $w->send; # wake up current and all future recv's $w->recv; # enters "main loop" till $condvar gets ->send + # use a condvar in callback mode: + $w->cb (sub { $_[0]->recv }); + +=head1 INTRODUCTION/TUTORIAL + +This manpage is mainly a reference manual. If you are interested +in a tutorial or some gentle introduction, have a look at the +L manpage. =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) @@ -29,11 +44,12 @@ policy> and AnyEvent is I. First and foremost, I itself, it only -interfaces to whatever event model the main program happens to use in a +interfaces to whatever event model the main program happens to use, in a pragmatic way. For event models and certain classes of immortals alike, the statement "there can only be one" is a bitter reality: In general, only one event loop can be active at the same time in a process. AnyEvent -helps hiding the differences between those event loops. +cannot change this, but it can hide the differences between those event +loops. The goal of AnyEvent is to offer module authors the ability to do event programming (waiting for I/O or timer events) without subscribing to a @@ -44,18 +60,18 @@ For modules like POE or IO::Async (which is a total misnomer as it is actually doing all I/O I...), using them in your module is like joining a cult: After you joined, you are dependent on them and you -cannot use anything else, as it is simply incompatible to everything that -isn't itself. What's worse, all the potential users of your module are -I forced to use the same event loop you use. +cannot use anything else, as they are simply incompatible to everything +that isn't them. What's worse, all the potential users of your +module are I forced to use the same event loop you use. AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works fine. AnyEvent + Tk works fine etc. etc. but none of these work together with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if your module uses one of those, every user of your module has to use it, too. But if your module uses AnyEvent, it works transparently with all -event models it supports (including stuff like POE and IO::Async, as long -as those use one of the supported event loops. It is trivial to add new -event loops to AnyEvent, too, so it is future-proof). +event models it supports (including stuff like IO::Async, as long as those +use one of the supported event loops. It is trivial to add new event loops +to AnyEvent, too, so it is future-proof). In addition to being free of having to use I, AnyEvent also is free of bloat and policy: with POE or similar @@ -134,10 +150,10 @@ An any way to achieve that is this pattern: - my $w; $w = AnyEvent->type (arg => value ..., cb => sub { - # you can use $w here, for example to undef it - undef $w; - }); + my $w; $w = AnyEvent->type (arg => value ..., cb => sub { + # you can use $w here, for example to undef it + undef $w; + }); Note that C combination. This is necessary because in Perl, my variables are only visible after the statement in which they are @@ -148,11 +164,11 @@ You can create an I/O watcher by calling the C<< AnyEvent->io >> method with the following mandatory key-value pairs as arguments: -C the Perl I (I file descriptor) to watch -for events. C must be a string that is either C or C, -which creates a watcher waiting for "r"eadable or "w"ritable events, -respectively. C is the callback to invoke each time the file handle -becomes ready. +C the Perl I (I file descriptor) to watch for events +(AnyEvent might or might not keep a reference to this file handle). C +must be a string that is either C or C, which creates a watcher +waiting for "r"eadable or "w"ritable events, respectively. C is the +callback to invoke each time the file handle becomes ready. Although the callback might get passed parameters, their value and presence is undefined and you cannot rely on them. Portable AnyEvent @@ -166,9 +182,9 @@ always use non-blocking calls when reading/writing from/to your file handles. -Example: +Example: wait for readability of STDIN, then read a line and disable the +watcher. - # wait for readability of STDIN, then read a line and disable the watcher my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { chomp (my $input = ); warn "read: $input\n"; @@ -188,13 +204,18 @@ presence is undefined and you cannot rely on them. Portable AnyEvent callbacks cannot use arguments passed to time watcher callbacks. -The timer callback will be invoked at most once: if you want a repeating -timer you have to create a new watcher (this is a limitation by both Tk -and Glib). +The callback will normally be invoked once only. If you specify another +parameter, C, as a strictly positive number (> 0), then the +callback will be invoked regularly at that interval (in fractional +seconds) after the first invocation. If C is specified with a +false value, then it is treated as if it were missing. + +The callback will be rescheduled before invoking the callback, but no +attempt is done to avoid timer drift in most backends, so the interval is +only approximate. -Example: +Example: fire an event after 7.7 seconds. - # fire an event after 7.7 seconds my $w = AnyEvent->timer (after => 7.7, cb => sub { warn "timeout\n"; }); @@ -202,19 +223,12 @@ # to cancel the timer: undef $w; -Example 2: - - # fire an event after 0.5 seconds, then roughly every second - my $w; +Example 2: fire an event after 0.5 seconds, then roughly every second. - my $cb = sub { - # cancel the old timer while creating a new one - $w = AnyEvent->timer (after => 1, cb => $cb); + my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { + warn "timeout\n"; }; - # start the "loop" by creating the first watcher - $w = AnyEvent->timer (after => 0.5, cb => $cb); - =head3 TIMING ISSUES There are two ways to handle timers: based on real time (relative, "fire @@ -301,8 +315,8 @@ =head2 SIGNAL WATCHERS You can watch for signals using a signal watcher, C is the signal -I without any C prefix, C is the Perl callback to -be invoked whenever a signal occurs. +I in uppercase and without any C prefix, C is the Perl +callback to be invoked whenever a signal occurs. Although the callback might get passed parameters, their value and presence is undefined and you cannot rely on them. Portable AnyEvent @@ -348,21 +362,21 @@ Example: fork a process and wait for it - my $done = AnyEvent->condvar; - - my $pid = fork or exit 5; - - my $w = AnyEvent->child ( - pid => $pid, - cb => sub { - my ($pid, $status) = @_; - warn "pid $pid exited with status $status"; - $done->send; - }, - ); - - # do something else, then wait for process exit - $done->recv; + my $done = AnyEvent->condvar; + + my $pid = fork or exit 5; + + my $w = AnyEvent->child ( + pid => $pid, + cb => sub { + my ($pid, $status) = @_; + warn "pid $pid exited with status $status"; + $done->send; + }, + ); + + # do something else, then wait for process exit + $done->recv; =head2 CONDITION VARIABLES @@ -378,8 +392,10 @@ Condition variables can be created by calling the C<< AnyEvent->condvar >> method, usually without arguments. The only argument pair allowed is + C, which specifies a callback to be called when the condition variable -becomes true. +becomes true, with the condition variable as the first argument (but not +the results). After creation, the condition variable is "false" until it becomes "true" by calling the C method (or calling the condition variable as if it @@ -445,6 +461,23 @@ my $delay = AnyEvent->timer (after => 5, cb => $done); $done->recv; +Example: Imagine an API that returns a condvar and doesn't support +callbacks. This is how you make a synchronous call, for example from +the main program: + + use AnyEvent::CouchDB; + + ... + + my @info = $couchdb->info->recv; + +And this is how you would just ste a callback to be called whenever the +results are available: + + $couchdb->info->cb (sub { + my @info = $_[0]->recv; + }); + =head3 METHODS FOR PRODUCERS These methods should only be used by the producing side, i.e. the @@ -587,14 +620,15 @@ Returns true when the condition is "true", i.e. whether C or C have been called. -=item $cb = $cv->cb ([new callback]) +=item $cb = $cv->cb ($cb->($cv)) This is a mutator function that returns the callback set and optionally replaces it before doing so. The callback will be called when the condition becomes "true", i.e. when -C or C are called. Calling C inside the callback -or at any later time is guaranteed not to block. +C or C are called, with the only argument being the condition +variable itself. Calling C inside the callback or at any later time +is guaranteed not to block. =back @@ -733,20 +767,27 @@ Contains various utility functions that replace often-used but blocking functions such as C by event-/callback-based versions. -=item L - -Provide read and write buffers and manages watchers for reads and writes. - =item L Provides various utility functions for (internet protocol) sockets, addresses and name resolution. Also functions to create non-blocking tcp connections or tcp servers, with IPv6 and SRV record support and more. +=item L + +Provide read and write buffers, manages watchers for reads and writes, +supports raw and formatted I/O, I/O queued and fully transparent and +non-blocking SSL/TLS. + =item L Provides rich asynchronous DNS resolver capabilities. +=item L + +A simple-to-use HTTP library that is capable of making a lot of concurrent +HTTP requests. + =item L Provides a simple web application server framework. @@ -755,6 +796,30 @@ The fastest ping in the west. +=item L + +Executes L requests asynchronously in a proxy process. + +=item L + +Truly asynchronous I/O, should be in the toolbox of every event +programmer. AnyEvent::AIO transparently fuses L and AnyEvent +together. + +=item L + +Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses +L and AnyEvent together. + +=item L + +A non-blocking interface to gpsd, a daemon delivering GPS information. + +=item L + +A non-blocking interface to the Internet Go Server protocol (used by +L). + =item L AnyEvent based IRC client module family. @@ -776,17 +841,6 @@ Has special support for AnyEvent via L. -=item L, L - -Truly asynchronous I/O, should be in the toolbox of every event -programmer. AnyEvent::AIO transparently fuses IO::AIO and AnyEvent -together. - -=item L, L - -Truly asynchronous Berkeley DB access. AnyEvent::AIO transparently fuses -IO::AIO and AnyEvent together. - =item L The lambda approach to I/O - don't ask, look there. Can use AnyEvent. @@ -802,7 +856,7 @@ use Carp; -our $VERSION = 4.11; +our $VERSION = 4.231; our $MODEL; our $AUTOLOAD; @@ -915,9 +969,12 @@ } } - unshift @ISA, $MODEL; push @{"$MODEL\::ISA"}, "AnyEvent::Base"; + unshift @ISA, $MODEL; + + require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; + (shift @post_detect)->() while @post_detect; } @@ -936,6 +993,27 @@ $class->$func (@_); } +# utility function to dup a filehandle. this is used by many backends +# to support binding more than one watcher per filehandle (they usually +# allow only one watcher per fd, so we dup it to get a different one). +sub _dupfh($$$$) { + my ($poll, $fh, $r, $w) = @_; + + require Fcntl; + + # cygwin requires the fh mode to be matching, unix doesn't + my ($rw, $mode) = $poll eq "r" ? ($r, "<") + : $poll eq "w" ? ($w, ">") + : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; + + open my $fh2, "$mode&" . fileno $fh + or die "cannot dup() filehandle: $!"; + + # we assume CLOEXEC is already set by perl in all important cases + + ($fh2, $rw) +} + package AnyEvent::Base; # default implementation for now and time @@ -974,7 +1052,7 @@ delete $SIG_CB{$signal}{$cb}; - $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; + delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; } # default implementation for ->child @@ -1154,6 +1232,19 @@ When set to C<2> or higher, cause AnyEvent to report to STDERR which event model it chooses. +=item C + +AnyEvent does not do much argument checking by default, as thorough +argument checking is very costly. Setting this variable to a true value +will cause AnyEvent to load C and then to thoroughly +check the arguments passed to most method calls. If it finds any problems +it will croak. + +In other words, enables "strict" mode. + +Unlike C it is definitely recommended ot keep it off in +production. + =item C This can be used to specify the event model to be used by AnyEvent, before @@ -1168,7 +1259,7 @@ For example, to force the pure perl model (L) you could start your program like this: - PERL_ANYEVENT_MODEL=Perl perl ... + PERL_ANYEVENT_MODEL=Perl perl ... =item C @@ -1653,13 +1744,23 @@ You can make AnyEvent completely ignore this variable by deleting it before the first watcher gets created, e.g. with a C block: - BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } - - use AnyEvent; + BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } + + use AnyEvent; Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can be used to probe what backend is used and gain other information (which is -probably even less useful to an attacker than PERL_ANYEVENT_MODEL). +probably even less useful to an attacker than PERL_ANYEVENT_MODEL), and +$ENV{PERL_ANYEGENT_STRICT}. + + +=head1 BUGS + +Perl 5.8 has numerous memleaks that sometimes hit this module and are hard +to work around. If you suffer from memleaks, first upgrade to Perl 5.10 +and check wether the leaks still show up. (Perl 5.10.0 has other annoying +mamleaks, such as leaking on C and C but it is usually not as +pronounced). =head1 SEE ALSO @@ -1686,8 +1787,8 @@ =head1 AUTHOR - Marc Lehmann - http://home.schmorp.de/ + Marc Lehmann + http://home.schmorp.de/ =cut