Changeset 20187 for lang/perl

Show
Ignore:
Timestamp:
09/29/08 13:09:04 (5 years ago)
Author:
daisuke
Message:

add a on_child_reap callback

Location:
lang/perl/Parallel-Prefork/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Parallel-Prefork/trunk/lib/Parallel/Prefork.pm

    r18842 r20187  
    88use Proc::Wait3; 
    99 
    10 __PACKAGE__->mk_accessors(qw/max_workers err_respawn_interval trap_signals signal_received manager_pid/); 
     10__PACKAGE__->mk_accessors(qw/max_workers err_respawn_interval trap_signals signal_received manager_pid on_child_reap/); 
    1111 
    1212our $VERSION = '0.03'; 
     
    6262        } 
    6363        if (my ($exit_pid, $status) = wait3(! $pid)) { 
     64            $self->_run_child_reap_cb( $exit_pid, $status ); 
     65 
    6466            if (delete($self->{worker_pids}{$exit_pid}) == $self->{generation} 
    65                     && $status != 0) { 
     67                && $status != 0) { 
    6668                sleep $self->err_respawn_interval; 
    6769            } 
     
    8991} 
    9092 
     93sub _run_child_reap_cb { 
     94    my ($self, $exit_pid, $status) = @_; 
     95    my $cb = $self->on_child_reap; 
     96    if ($cb) { 
     97        eval { 
     98            $cb->($self, $exit_pid, $status); 
     99        }; 
     100        # XXX - hmph, what to do here? 
     101    } 
     102} 
     103 
    91104sub wait_all_children { 
    92105    my $self = shift; 
    93106    while (%{$self->{worker_pids}}) { 
    94107        if (my $pid = wait) { 
    95             delete $self->{worker_pids}{$pid}; 
     108            if (delete $self->{worker_pids}{$pid}) { 
     109                $self->_run_child_reap_cb($pid, $?); 
     110            } 
    96111        } 
    97112    } 
     
    152167hashref of signals to be trapped.  Manager process will trap the signals listed in the keys of the hash, and send the signal specified in the associated value (if any) to all worker processes. 
    153168 
     169=head3 on_child_reap 
     170 
     171Coderef that is called when a child is reaped. Receives the instance to 
     172the current Paralle::Prefork, the child's pid, and its exit status. 
     173 
    154174=head2 start 
    155175 
  • lang/perl/Parallel-Prefork/trunk/t/01-base.t

    r8848 r20187  
    66use Fcntl qw/:flock/; 
    77use File::Temp qw/tempfile/; 
    8 use Test::More tests => 4; 
     8use Test::More tests => 5; 
    99 
    1010use_ok('Parallel::Prefork'); 
    1111 
     12my $reaped = 0; 
    1213my $pm; 
    1314eval { 
    1415    $pm = Parallel::Prefork->new({ 
    15         max_workers => 10, 
    16         fork_delay  => 0, 
     16        max_workers   => 10, 
     17        fork_delay    => 0, 
     18        on_child_reap => sub { 
     19            $reaped++; 
     20        } 
    1721    }); 
    1822}; 
     
    6165close $fh; 
    6266is($c, $pm->max_workers * 2); 
     67is($reaped, $pm->max_workers, "properly called on_child_reap callback"); 
    6368 
    6469unlink $filename;