… | |
… | |
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 | trigger as often as status change for the child are received. This works |
180 | installing a signal handler for C<SIGCHLD>. |
181 | by installing a signal handler for C<SIGCHLD>. |
181 | |
182 | |
182 | Example: wait for pid 1333 |
183 | Example: wait for pid 1333 |
183 | |
184 | |
184 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
185 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
185 | |
186 | |
… | |
… | |
195 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
196 | 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>). |
197 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
197 | |
198 | |
198 | The known classes so far are: |
199 | The known classes so far are: |
199 | |
200 | |
|
|
201 | EV::AnyEvent based on EV (an interface to libev, best choice) |
200 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
202 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
201 | AnyEvent::Impl::Event based on Event, also best choice :) |
203 | AnyEvent::Impl::Event based on Event, also second best choice :) |
202 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
204 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
203 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
205 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
204 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
206 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
205 | |
207 | |
206 | =item AnyEvent::detect |
208 | =item AnyEvent::detect |
… | |
… | |
248 | no warnings; |
250 | no warnings; |
249 | use strict; |
251 | use strict; |
250 | |
252 | |
251 | use Carp; |
253 | use Carp; |
252 | |
254 | |
253 | our $VERSION = '2.52'; |
255 | our $VERSION = '2.55'; |
254 | our $MODEL; |
256 | our $MODEL; |
255 | |
257 | |
256 | our $AUTOLOAD; |
258 | our $AUTOLOAD; |
257 | our @ISA; |
259 | our @ISA; |
258 | |
260 | |
… | |
… | |
260 | |
262 | |
261 | our @REGISTRY; |
263 | our @REGISTRY; |
262 | |
264 | |
263 | my @models = ( |
265 | my @models = ( |
264 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
266 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
|
|
267 | [EV:: => EV::AnyEvent::], |
265 | [Event:: => AnyEvent::Impl::Event::], |
268 | [Event:: => AnyEvent::Impl::Event::], |
266 | [Glib:: => AnyEvent::Impl::Glib::], |
269 | [Glib:: => AnyEvent::Impl::Glib::], |
267 | [Tk:: => AnyEvent::Impl::Tk::], |
270 | [Tk:: => AnyEvent::Impl::Tk::], |
268 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
271 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
269 | ); |
272 | ); |
… | |
… | |
372 | our $PID_IDLE; |
375 | our $PID_IDLE; |
373 | our $WNOHANG; |
376 | our $WNOHANG; |
374 | |
377 | |
375 | sub _child_wait { |
378 | sub _child_wait { |
376 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
379 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
377 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
380 | $_->() for (values %{ $PID_CB{$pid} || {} }), |
|
|
381 | (values %{ $PID_CB{0} || {} }); |
378 | } |
382 | } |
379 | |
383 | |
380 | undef $PID_IDLE; |
384 | undef $PID_IDLE; |
381 | } |
385 | } |
382 | |
386 | |
383 | sub child { |
387 | sub child { |
384 | my (undef, %arg) = @_; |
388 | my (undef, %arg) = @_; |
385 | |
389 | |
386 | my $pid = uc $arg{pid} |
390 | defined (my $pid = $arg{pid} + 0) |
387 | or Carp::croak "required option 'pid' is missing"; |
391 | or Carp::croak "required option 'pid' is missing"; |
388 | |
392 | |
389 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
393 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
390 | |
394 | |
391 | unless ($WNOHANG) { |
395 | unless ($WNOHANG) { |
… | |
… | |
440 | I<rxvt-unicode> distribution. |
444 | I<rxvt-unicode> distribution. |
441 | |
445 | |
442 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
446 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
443 | condition variables: code blocking while waiting for a condition will |
447 | condition variables: code blocking while waiting for a condition will |
444 | C<die>. This still works with most modules/usages, and blocking calls must |
448 | C<die>. This still works with most modules/usages, and blocking calls must |
445 | not be in an interactive appliation, so it makes sense. |
449 | not be in an interactive application, so it makes sense. |
446 | |
450 | |
447 | =head1 ENVIRONMENT VARIABLES |
451 | =head1 ENVIRONMENT VARIABLES |
448 | |
452 | |
449 | The following environment variables are used by this module: |
453 | The following environment variables are used by this module: |
450 | |
454 | |