… | |
… | |
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 | |
… | |
… | |
373 | our $CHLD_W; |
375 | our $CHLD_W; |
374 | our $PID_IDLE; |
376 | our $PID_IDLE; |
375 | our $WNOHANG; |
377 | our $WNOHANG; |
376 | |
378 | |
377 | sub _child_wait { |
379 | sub _child_wait { |
378 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
380 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
379 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
381 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
|
|
382 | (values %{ $PID_CB{0} || {} }); |
380 | } |
383 | } |
381 | |
384 | |
382 | undef $PID_IDLE; |
385 | undef $PID_IDLE; |
383 | } |
386 | } |
384 | |
387 | |
385 | sub child { |
388 | sub child { |
386 | my (undef, %arg) = @_; |
389 | my (undef, %arg) = @_; |
387 | |
390 | |
388 | my $pid = uc $arg{pid} |
391 | defined (my $pid = $arg{pid} + 0) |
389 | or Carp::croak "required option 'pid' is missing"; |
392 | or Carp::croak "required option 'pid' is missing"; |
390 | |
393 | |
391 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
394 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
392 | |
395 | |
393 | unless ($WNOHANG) { |
396 | unless ($WNOHANG) { |