… | |
… | |
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 (or any child if the pid argument is 0). The watcher will |
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 | trigger as often as status change for the child are received. This works |
181 | by installing a signal handler for C<SIGCHLD>. |
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). |
182 | |
183 | |
183 | Example: wait for pid 1333 |
184 | Example: 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 | |
… | |
… | |
374 | our $CHLD_W; |
375 | our $CHLD_W; |
375 | our $PID_IDLE; |
376 | our $PID_IDLE; |
376 | our $WNOHANG; |
377 | our $WNOHANG; |
377 | |
378 | |
378 | sub _child_wait { |
379 | sub _child_wait { |
379 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
380 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
380 | $_->() for (values %{ $PID_CB{$pid} || {} }), |
381 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
381 | (values %{ $PID_CB{0} || {} }); |
382 | (values %{ $PID_CB{0} || {} }); |
382 | } |
383 | } |
383 | |
384 | |
384 | undef $PID_IDLE; |
385 | undef $PID_IDLE; |
385 | } |
386 | } |
386 | |
387 | |