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.22 by root, Sun Dec 31 11:54:43 2006 UTC vs.
Revision 1.36 by root, Fri Nov 16 09:13:11 2007 UTC

70You can create I/O watcher by calling the C<< AnyEvent->io >> method with 70You can create I/O watcher by calling the C<< AnyEvent->io >> method with
71the following mandatory arguments: 71the following mandatory arguments:
72 72
73C<fh> the Perl I<filehandle> (not filedescriptor) to watch for 73C<fh> the Perl I<filehandle> (not filedescriptor) to watch for
74events. C<poll> must be a string that is either C<r> or C<w>, that creates 74events. C<poll> must be a string that is either C<r> or C<w>, that creates
75a watcher waiting for "r"eadable or "w"ritable events. C<cb> teh callback 75a watcher waiting for "r"eadable or "w"ritable events. C<cb> the callback
76to invoke everytime the filehandle becomes ready. 76to invoke everytime the filehandle becomes ready.
77 77
78Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on 78Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on
79a socket you can have one r + one w, not any more (limitation comes from 79a socket you can have one r + one w, not any more (limitation comes from
80Tk - if you are sure you are not using Tk this limitation is gone). 80Tk - if you are sure you are not using Tk this limitation is gone).
174 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); 174 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
175 175
176=head2 CHILD PROCESS WATCHERS 176=head2 CHILD PROCESS WATCHERS
177 177
178You can also listen for the status of a child process specified by the 178You can also listen for the status of a child process specified by the
179C<pid> argument. The watcher will only trigger once. This works by 179C<pid> argument (or any child if the pid argument is 0). The watcher will
180installing a signal handler for C<SIGCHLD>. 180trigger as often as status change for the child are received. This works
181by installing a signal handler for C<SIGCHLD>. The callback will be called with
182the pid and exit status (as returned by waitpid).
181 183
182Example: wait for pid 1333 184Example: wait for pid 1333
183 185
184 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); 186 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" });
185 187
195C<AnyEvent::Impl:xxx> modules, but can be any other class in the case 197C<AnyEvent::Impl:xxx> modules, but can be any other class in the case
196AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). 198AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
197 199
198The known classes so far are: 200The known classes so far are:
199 201
202 AnyEvent::Impl::CoroEV based on Coro::EV, best choice.
203 AnyEvent::Impl::EV based on EV (an interface to libev, also best choice).
200 AnyEvent::Impl::Coro based on Coro::Event, best choise. 204 AnyEvent::Impl::Coro based on Coro::Event, second best choice.
201 AnyEvent::Impl::Event based on Event, also best choice :) 205 AnyEvent::Impl::Event based on Event, also second best choice :)
202 AnyEvent::Impl::Glib based on Glib, second-best choice. 206 AnyEvent::Impl::Glib based on Glib, second-best choice.
203 AnyEvent::Impl::Tk based on Tk, very bad choice. 207 AnyEvent::Impl::Tk based on Tk, very bad choice.
204 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 208 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
205 209
206=item AnyEvent::detect 210=item AnyEvent::detect
245 249
246package AnyEvent; 250package AnyEvent;
247 251
248no warnings; 252no warnings;
249use strict; 253use strict;
254
250use Carp; 255use Carp;
251 256
252our $VERSION = '2.51'; 257our $VERSION = '2.6';
253our $MODEL; 258our $MODEL;
254 259
255our $AUTOLOAD; 260our $AUTOLOAD;
256our @ISA; 261our @ISA;
257 262
258our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 263our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
259 264
260our @REGISTRY; 265our @REGISTRY;
261 266
262my @models = ( 267my @models = (
268 [Coro::EV:: => AnyEvent::Impl::CoroEV::],
269 [EV:: => AnyEvent::Impl::EV::],
263 [Coro::Event:: => AnyEvent::Impl::Coro::], 270 [Coro::Event:: => AnyEvent::Impl::Coro::],
264 [Event:: => AnyEvent::Impl::Event::], 271 [Event:: => AnyEvent::Impl::Event::],
265 [Glib:: => AnyEvent::Impl::Glib::], 272 [Glib:: => AnyEvent::Impl::Glib::],
266 [Tk:: => AnyEvent::Impl::Tk::], 273 [Tk:: => AnyEvent::Impl::Tk::],
267 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 274 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
298 last; 305 last;
299 } 306 }
300 } 307 }
301 308
302 $MODEL 309 $MODEL
303 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk."; 310 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.";
304 } 311 }
305 312
306 unshift @ISA, $MODEL; 313 unshift @ISA, $MODEL;
307 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 314 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
308 } 315 }
370our $CHLD_W; 377our $CHLD_W;
371our $PID_IDLE; 378our $PID_IDLE;
372our $WNOHANG; 379our $WNOHANG;
373 380
374sub _child_wait { 381sub _child_wait {
375 while (0 < (my $pid = waitpid -1, $WNOHANG)) { 382 while (0 <= (my $pid = waitpid -1, $WNOHANG)) {
376 $_->() for values %{ (delete $PID_CB{$pid}) || {} }; 383 $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }),
384 (values %{ $PID_CB{0} || {} });
377 } 385 }
378 386
379 undef $PID_IDLE; 387 undef $PID_IDLE;
380} 388}
381 389
382sub child { 390sub child {
383 my (undef, %arg) = @_; 391 my (undef, %arg) = @_;
384 392
385 my $pid = uc $arg{pid} 393 defined (my $pid = $arg{pid} + 0)
386 or Carp::croak "required option 'pid' is missing"; 394 or Carp::croak "required option 'pid' is missing";
387 395
388 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 396 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
389 397
390 unless ($WNOHANG) { 398 unless ($WNOHANG) {
391 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
392 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; 399 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
393 } 400 }
394 401
402 unless ($CHLD_W) {
403 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
395 # child could be a zombie already 404 # child could be a zombie already
396 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); 405 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait);
406 }
397 407
398 bless [$pid, $arg{cb}], "AnyEvent::Base::Child" 408 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
399} 409}
400 410
401sub AnyEvent::Base::Child::DESTROY { 411sub AnyEvent::Base::Child::DESTROY {
437I<rxvt-unicode> distribution. 447I<rxvt-unicode> distribution.
438 448
439I<rxvt-unicode> also cheats a bit by not providing blocking access to 449I<rxvt-unicode> also cheats a bit by not providing blocking access to
440condition variables: code blocking while waiting for a condition will 450condition variables: code blocking while waiting for a condition will
441C<die>. This still works with most modules/usages, and blocking calls must 451C<die>. This still works with most modules/usages, and blocking calls must
442not be in an interactive appliation, so it makes sense. 452not be in an interactive application, so it makes sense.
443 453
444=head1 ENVIRONMENT VARIABLES 454=head1 ENVIRONMENT VARIABLES
445 455
446The following environment variables are used by this module: 456The following environment variables are used by this module:
447 457

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines