… | |
… | |
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 | EV::AnyEvent based on EV (an interface to libev, best choice) |
200 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
203 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
201 | AnyEvent::Impl::Event based on Event, also best choice :) |
204 | AnyEvent::Impl::Event based on Event, also second best choice :) |
202 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
205 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
203 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
206 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
204 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
207 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
205 | |
208 | |
206 | =item AnyEvent::detect |
209 | =item AnyEvent::detect |
… | |
… | |
245 | |
248 | |
246 | package AnyEvent; |
249 | package AnyEvent; |
247 | |
250 | |
248 | no warnings; |
251 | no warnings; |
249 | use strict; |
252 | use strict; |
|
|
253 | |
250 | use Carp; |
254 | use Carp; |
251 | |
255 | |
252 | our $VERSION = '2.52'; |
256 | our $VERSION = '2.55'; |
253 | our $MODEL; |
257 | our $MODEL; |
254 | |
258 | |
255 | our $AUTOLOAD; |
259 | our $AUTOLOAD; |
256 | our @ISA; |
260 | our @ISA; |
257 | |
261 | |
… | |
… | |
259 | |
263 | |
260 | our @REGISTRY; |
264 | our @REGISTRY; |
261 | |
265 | |
262 | my @models = ( |
266 | my @models = ( |
263 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
267 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
|
|
268 | [EV:: => EV::AnyEvent::], |
264 | [Event:: => AnyEvent::Impl::Event::], |
269 | [Event:: => AnyEvent::Impl::Event::], |
265 | [Glib:: => AnyEvent::Impl::Glib::], |
270 | [Glib:: => AnyEvent::Impl::Glib::], |
266 | [Tk:: => AnyEvent::Impl::Tk::], |
271 | [Tk:: => AnyEvent::Impl::Tk::], |
267 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
272 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
268 | ); |
273 | ); |
… | |
… | |
370 | our $CHLD_W; |
375 | our $CHLD_W; |
371 | our $PID_IDLE; |
376 | our $PID_IDLE; |
372 | our $WNOHANG; |
377 | our $WNOHANG; |
373 | |
378 | |
374 | sub _child_wait { |
379 | sub _child_wait { |
375 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
380 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
376 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
381 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
|
|
382 | (values %{ $PID_CB{0} || {} }); |
377 | } |
383 | } |
378 | |
384 | |
379 | undef $PID_IDLE; |
385 | undef $PID_IDLE; |
380 | } |
386 | } |
381 | |
387 | |
382 | sub child { |
388 | sub child { |
383 | my (undef, %arg) = @_; |
389 | my (undef, %arg) = @_; |
384 | |
390 | |
385 | my $pid = uc $arg{pid} |
391 | defined (my $pid = $arg{pid} + 0) |
386 | or Carp::croak "required option 'pid' is missing"; |
392 | or Carp::croak "required option 'pid' is missing"; |
387 | |
393 | |
388 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
394 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
389 | |
395 | |
390 | unless ($WNOHANG) { |
396 | unless ($WNOHANG) { |
… | |
… | |
439 | I<rxvt-unicode> distribution. |
445 | I<rxvt-unicode> distribution. |
440 | |
446 | |
441 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
447 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
442 | condition variables: code blocking while waiting for a condition will |
448 | condition variables: code blocking while waiting for a condition will |
443 | C<die>. This still works with most modules/usages, and blocking calls must |
449 | C<die>. This still works with most modules/usages, and blocking calls must |
444 | not be in an interactive appliation, so it makes sense. |
450 | not be in an interactive application, so it makes sense. |
445 | |
451 | |
446 | =head1 ENVIRONMENT VARIABLES |
452 | =head1 ENVIRONMENT VARIABLES |
447 | |
453 | |
448 | The following environment variables are used by this module: |
454 | The following environment variables are used by this module: |
449 | |
455 | |