… | |
… | |
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 | |
… | |
… | |
349 | my (undef, %arg) = @_; |
350 | my (undef, %arg) = @_; |
350 | |
351 | |
351 | my $signal = uc $arg{signal} |
352 | my $signal = uc $arg{signal} |
352 | or Carp::croak "required option 'signal' is missing"; |
353 | or Carp::croak "required option 'signal' is missing"; |
353 | |
354 | |
354 | $SIG_CB{$signal}{$arg{cb} += 0} = $arg{cb}; |
355 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
355 | $SIG{$signal} ||= sub { |
356 | $SIG{$signal} ||= sub { |
356 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
357 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
357 | }; |
358 | }; |
358 | |
359 | |
359 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
360 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
… | |
… | |
374 | our $PID_IDLE; |
375 | our $PID_IDLE; |
375 | our $WNOHANG; |
376 | our $WNOHANG; |
376 | |
377 | |
377 | sub _child_wait { |
378 | sub _child_wait { |
378 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
379 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
379 | $_->() for values %{ $PID_CB{$pid} || {} }, %{ $PID_CB{0} || {} }; |
380 | $_->() for (values %{ $PID_CB{$pid} || {} }), |
|
|
381 | (values %{ $PID_CB{0} || {} }); |
380 | } |
382 | } |
381 | |
383 | |
382 | undef $PID_IDLE; |
384 | undef $PID_IDLE; |
383 | } |
385 | } |
384 | |
386 | |
385 | sub child { |
387 | sub child { |
386 | my (undef, %arg) = @_; |
388 | my (undef, %arg) = @_; |
387 | |
389 | |
388 | my $pid = uc $arg{pid} |
390 | defined (my $pid = $arg{pid} + 0) |
389 | or Carp::croak "required option 'pid' is missing"; |
391 | or Carp::croak "required option 'pid' is missing"; |
390 | |
392 | |
391 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
393 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
392 | |
394 | |
393 | unless ($WNOHANG) { |
395 | unless ($WNOHANG) { |