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.31 by root, Fri Nov 2 19:20:36 2007 UTC vs.
Revision 1.38 by root, Sun Nov 25 14:08:12 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).
109 my $w = AnyEvent->timer (after => 7.7, cb => sub { 109 my $w = AnyEvent->timer (after => 7.7, cb => sub {
110 warn "timeout\n"; 110 warn "timeout\n";
111 }); 111 });
112 112
113 # to cancel the timer: 113 # to cancel the timer:
114 undef $w 114 undef $w;
115 115
116=head2 CONDITION WATCHERS 116=head2 CONDITION WATCHERS
117 117
118Condition watchers can be created by calling the C<< AnyEvent->condvar >> 118Condition watchers can be created by calling the C<< AnyEvent->condvar >>
119method without any arguments. 119method without any arguments.
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 (or any child if the pid argument is 0). The watcher will 179C<pid> argument (or any child if the pid argument is 0). The watcher will
180trigger as often as status change for the child are received. This works 180trigger as often as status change for the child are received. This works
181by installing a signal handler for C<SIGCHLD>. 181by installing a signal handler for C<SIGCHLD>. The callback will be called with
182the pid and exit status (as returned by waitpid).
182 183
183Example: wait for pid 1333 184Example: wait for pid 1333
184 185
185 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); 186 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" });
186 187
196C<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
197AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). 198AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
198 199
199The known classes so far are: 200The known classes so far are:
200 201
202 AnyEvent::Impl::CoroEV based on Coro::EV, best choice.
201 EV::AnyEvent based on EV (an interface to libev, best choice) 203 AnyEvent::Impl::EV based on EV (an interface to libev, also best choice).
202 AnyEvent::Impl::Coro based on Coro::Event, second best choice. 204 AnyEvent::Impl::Coro based on Coro::Event, second best choice.
203 AnyEvent::Impl::Event based on Event, also second best choice :) 205 AnyEvent::Impl::Event based on Event, also second best choice :)
204 AnyEvent::Impl::Glib based on Glib, second-best choice. 206 AnyEvent::Impl::Glib based on Glib, second-best choice.
205 AnyEvent::Impl::Tk based on Tk, very bad choice. 207 AnyEvent::Impl::Tk based on Tk, very bad choice.
206 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 208 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
250no warnings; 252no warnings;
251use strict; 253use strict;
252 254
253use Carp; 255use Carp;
254 256
255our $VERSION = '2.55'; 257our $VERSION = '2.8';
256our $MODEL; 258our $MODEL;
257 259
258our $AUTOLOAD; 260our $AUTOLOAD;
259our @ISA; 261our @ISA;
260 262
261our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 263our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
262 264
263our @REGISTRY; 265our @REGISTRY;
264 266
265my @models = ( 267my @models = (
268 [Coro::EV:: => AnyEvent::Impl::CoroEV::],
269 [EV:: => AnyEvent::Impl::EV::],
266 [Coro::Event:: => AnyEvent::Impl::Coro::], 270 [Coro::Event:: => AnyEvent::Impl::Coro::],
267 [EV:: => EV::AnyEvent::],
268 [Event:: => AnyEvent::Impl::Event::], 271 [Event:: => AnyEvent::Impl::Event::],
269 [Glib:: => AnyEvent::Impl::Glib::], 272 [Glib:: => AnyEvent::Impl::Glib::],
270 [Tk:: => AnyEvent::Impl::Tk::], 273 [Tk:: => AnyEvent::Impl::Tk::],
271 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 274 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
272); 275);
302 last; 305 last;
303 } 306 }
304 } 307 }
305 308
306 $MODEL 309 $MODEL
307 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.";
308 } 311 }
309 312
310 unshift @ISA, $MODEL; 313 unshift @ISA, $MODEL;
311 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 314 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
312 } 315 }
370 373
371# default implementation for ->child 374# default implementation for ->child
372 375
373our %PID_CB; 376our %PID_CB;
374our $CHLD_W; 377our $CHLD_W;
378our $CHLD_DELAY_W;
375our $PID_IDLE; 379our $PID_IDLE;
376our $WNOHANG; 380our $WNOHANG;
377 381
378sub _child_wait { 382sub _child_wait {
379 while (0 < (my $pid = waitpid -1, $WNOHANG)) { 383 while (0 < (my $pid = waitpid -1, $WNOHANG)) {
380 $_->() for (values %{ $PID_CB{$pid} || {} }), 384 $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }),
381 (values %{ $PID_CB{0} || {} }); 385 (values %{ $PID_CB{0} || {} });
382 } 386 }
383 387
384 undef $PID_IDLE; 388 undef $PID_IDLE;
389}
390
391sub _sigchld {
392 # make sure we deliver these changes "synchronous" with the event loop.
393 $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub {
394 undef $CHLD_DELAY_W;
395 &_child_wait;
396 });
385} 397}
386 398
387sub child { 399sub child {
388 my (undef, %arg) = @_; 400 my (undef, %arg) = @_;
389 401
395 unless ($WNOHANG) { 407 unless ($WNOHANG) {
396 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; 408 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
397 } 409 }
398 410
399 unless ($CHLD_W) { 411 unless ($CHLD_W) {
400 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); 412 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld);
401 # child could be a zombie already 413 # child could be a zombie already, so make at least one round
402 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); 414 &_sigchld;
403 } 415 }
404 416
405 bless [$pid, $arg{cb}], "AnyEvent::Base::Child" 417 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
406} 418}
407 419

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines