… | |
… | |
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
71 | the following mandatory arguments: |
71 | the following mandatory arguments: |
72 | |
72 | |
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> teh callback |
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> the callback |
76 | to invoke everytime the filehandle becomes ready. |
76 | to invoke everytime the filehandle becomes ready. |
77 | |
77 | |
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
79 | a socket you can have one r + one w, not any more (limitation comes from |
79 | a socket you can have one r + one w, not any more (limitation comes from |
80 | Tk - if you are sure you are not using Tk this limitation is gone). |
80 | Tk - 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 | |
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
119 | method without any arguments. |
119 | method without any arguments. |
… | |
… | |
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 | |
178 | You can also listen for the status of a child process specified by the |
178 | You can also listen for the status of a child process specified by the |
179 | C<pid> argument. The watcher will only trigger once. This works by |
179 | C<pid> argument (or any child if the pid argument is 0). The watcher will |
180 | installing a signal handler for C<SIGCHLD>. |
180 | trigger as often as status change for the child are received. This works |
|
|
181 | by installing a signal handler for C<SIGCHLD>. The callback will be called with |
|
|
182 | the pid and exit status (as returned by waitpid). |
181 | |
183 | |
182 | Example: wait for pid 1333 |
184 | Example: 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 | |
… | |
… | |
195 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
197 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
196 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
198 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
197 | |
199 | |
198 | The known classes so far are: |
200 | The 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 |
… | |
… | |
248 | no warnings; |
252 | no warnings; |
249 | use strict; |
253 | use strict; |
250 | |
254 | |
251 | use Carp; |
255 | use Carp; |
252 | |
256 | |
253 | our $VERSION = '2.53'; |
257 | our $VERSION = '2.7'; |
254 | our $MODEL; |
258 | our $MODEL; |
255 | |
259 | |
256 | our $AUTOLOAD; |
260 | our $AUTOLOAD; |
257 | our @ISA; |
261 | our @ISA; |
258 | |
262 | |
259 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
263 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
260 | |
264 | |
261 | our @REGISTRY; |
265 | our @REGISTRY; |
262 | |
266 | |
263 | my @models = ( |
267 | my @models = ( |
|
|
268 | [Coro::EV:: => AnyEvent::Impl::CoroEV::], |
|
|
269 | [EV:: => AnyEvent::Impl::EV::], |
264 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
270 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
265 | [Event:: => AnyEvent::Impl::Event::], |
271 | [Event:: => AnyEvent::Impl::Event::], |
266 | [Glib:: => AnyEvent::Impl::Glib::], |
272 | [Glib:: => AnyEvent::Impl::Glib::], |
267 | [Tk:: => AnyEvent::Impl::Tk::], |
273 | [Tk:: => AnyEvent::Impl::Tk::], |
268 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
274 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
… | |
… | |
299 | last; |
305 | last; |
300 | } |
306 | } |
301 | } |
307 | } |
302 | |
308 | |
303 | $MODEL |
309 | $MODEL |
304 | 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."; |
305 | } |
311 | } |
306 | |
312 | |
307 | unshift @ISA, $MODEL; |
313 | unshift @ISA, $MODEL; |
308 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
314 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
309 | } |
315 | } |
… | |
… | |
367 | |
373 | |
368 | # default implementation for ->child |
374 | # default implementation for ->child |
369 | |
375 | |
370 | our %PID_CB; |
376 | our %PID_CB; |
371 | our $CHLD_W; |
377 | our $CHLD_W; |
|
|
378 | our $CHLD_DELAY_W; |
372 | our $PID_IDLE; |
379 | our $PID_IDLE; |
373 | our $WNOHANG; |
380 | our $WNOHANG; |
374 | |
381 | |
375 | sub _child_wait { |
382 | sub _child_wait { |
376 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
383 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
377 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
384 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
|
|
385 | (values %{ $PID_CB{0} || {} }); |
378 | } |
386 | } |
379 | |
387 | |
380 | undef $PID_IDLE; |
388 | undef $PID_IDLE; |
|
|
389 | } |
|
|
390 | |
|
|
391 | sub _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 | }); |
381 | } |
397 | } |
382 | |
398 | |
383 | sub child { |
399 | sub child { |
384 | my (undef, %arg) = @_; |
400 | my (undef, %arg) = @_; |
385 | |
401 | |
386 | my $pid = uc $arg{pid} |
402 | defined (my $pid = $arg{pid} + 0) |
387 | or Carp::croak "required option 'pid' is missing"; |
403 | or Carp::croak "required option 'pid' is missing"; |
388 | |
404 | |
389 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
405 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
390 | |
406 | |
391 | unless ($WNOHANG) { |
407 | unless ($WNOHANG) { |
392 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
408 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
393 | } |
409 | } |
394 | |
410 | |
395 | unless ($CHLD_W) { |
411 | unless ($CHLD_W) { |
396 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); |
412 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
397 | # child could be a zombie already |
413 | # child could be a zombie already, so make at least one round |
398 | $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); |
414 | &_sigchld; |
399 | } |
415 | } |
400 | |
416 | |
401 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
417 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
402 | } |
418 | } |
403 | |
419 | |