| 1 | package Parallel::Runner; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use Time::HiRes qw/sleep/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use Child qw/child/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 25 | our $VERSION = '0.014'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 25 | for my $accessor (qw/ exit_callback data_callback iteration_callback _children pid max iteration_delay reap_callback pipe/) { | |||||||||||||||||||||||||||||||||||||||||
| 13 | 19598 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 19598 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 19598 | ▶ | ( $self->{$accessor} ) = @_ if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 19598 | return $self->{$accessor}; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 225 | }; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 25 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 225 | *$accessor = $sub; | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 2917 | ▶ | sub children { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
children: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 2917 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 2917 | my @active; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 2917 | ▶ | for my $proc ( @{$self->_children || []}, @_ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_children || []
MC/DC100%1/1
$self->_children || []
$self->_children
Truth table100%2/2
A
A= $self->_children
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 6891 | ▶ | if ( defined $proc->exit_status ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 100 | ▶ | if ( $self->data_callback ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | my $data = $proc->read(); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 8 | $self->data_callback->($data); | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 100 | ▶ | $self->reap_callback->( $proc->exit_status, $proc->pid, $proc->pid, $proc ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | if $self->reap_callback; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 100 | next; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 6791 | push @active => $proc; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2917 | $self->_children( \@active ); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 2917 | return @active; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 58 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 58 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 58 | my ($max) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 58 | ▶ | return bless( | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$max || 1
MC/DC100%1/1
$max || 1
$max
Truth table100%2/2
A
A= $max
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | _children => [], | ||||||||||||||||||||||||||||||||||||||||||
| 51 | pid => $$, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | max => $max || 1, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | iteration_delay => 0.1, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | @_, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | $class | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 134 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 134 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 134 | my ( $code, $force_fork ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 134 | ▶ | croak("Called run() in child process") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | unless $self->pid == $$; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 124 | ▶ | my $fork = $force_fork || $self->max > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$force_fork || $self->max > 1
MC/DC100%2/2
$force_fork || $self->max > 1
$force_fork
$self->max > 1
Truth table100%3/3
A || B
A= $force_forkB= $self->max > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 124 | ▶ | return $self->_fork($code) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | if $fork; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 10 | my ($data) = $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | $self->data_callback->($data) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | if $self->data_callback; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 114 | ▶ | sub _fork { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fork: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 114 | my ($code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # Wait for a slot | ||||||||||||||||||||||||||||||||||||||||||
| 82 | $self->_iterate( | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 2503 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 2503 | $self->children >= $self->max; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 114 | ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | my $proc = Child->new( | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 17 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 17 | my $parent = shift; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 17 | $self->_children( [] ); | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 17 | my @return = $code->($parent); | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 17 | ▶ | $self->exit_callback->(@return) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | if $self->exit_callback; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 17 | ▶ | $parent->write( $return[0] ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | if $self->data_callback; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 100 | ▶ | $self->pipe || $self->data_callback ? ( pipe => $self->pipe ) : () | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->pipe or $self->data_callback
MC/DC100%2/2
$self->pipe or $self->data_callback
$self->pipe
$self->data_callback
Truth table100%3/3
A or B
A= $self->pipeB= $self->data_callback
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | )->start(); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 555 | ▶ | $self->_iterate( sub { !defined $proc->exit_status } ) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 83 | ▶ | if $self->max == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 81 | ▶ | $self->children($proc) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | unless defined $proc->exit_status; | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 81 | return $proc; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 60 | ▶ | sub finish { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finish: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 60 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 60 | ▶ | $self->_iterate( sub { $self->children }, @_ ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 193 | ▶ | sub _iterate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_iterate: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 193 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 193 | my ( $condition, $timeout, $timeoutsub ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 193 | my $counter = 0; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 193 | ▶ | while ( $condition->() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 3151 | ▶ | $self->iteration_callback->($self) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | if $self->iteration_callback; | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 3151 | $counter += $self->iteration_delay; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 3151 | ▶ | last if $timeout and $counter >= $timeout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$timeout and $counter >= $timeout
MC/DC100%2/2
$timeout and $counter >= $timeout
$timeout
$counter >= $timeout
Truth table100%3/3
A and B
A= $timeoutB= $counter >= $timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 3150 | sleep $self->iteration_delay; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 177 | ▶ | $timeoutsub->() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$timeout and $timeoutsub and $counter >= $timeout
$timeout and $timeoutsub
MC/DC66%2/3
$timeout and $timeoutsub and $counter >= $timeout
$timeout
$timeoutsub
$counter >= $timeout
Truth table75%3/4
A and B and C
A= $timeoutB= $timeoutsubC= $counter >= $timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | if $timeout | ||||||||||||||||||||||||||||||||||||||||||
| 136 | && $timeoutsub | ||||||||||||||||||||||||||||||||||||||||||
| 137 | && $counter >= $timeout; | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 177 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | ▶ | sub killall { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
killall: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | my ( $sig, $warn ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | if ($warn) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | warn time . " - Killing: $_ - $sig\n" for grep { $_->pid } $self->children; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | $_->kill($sig) for $self->children; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 58 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 58 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 154 | return | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 58 | ▶ | unless $self->pid == $$ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->pid == $$ and $self->children
MC/DC100%2/2
$self->pid == $$ and $self->children
$self->pid == $$
$self->children
Truth table100%3/3
A and B
A= $self->pid == $$B= $self->children
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | && $self->children; | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | warn <<EOT; | |||||||||||||||||||||||||||||||||||||||||
| 158 | Parallel::Runner object destroyed without first calling finish(), This will | ||||||||||||||||||||||||||||||||||||||||||
| 159 | terminate all your child processes. This either means you forgot to call | ||||||||||||||||||||||||||||||||||||||||||
| 160 | finish() or your parent process has died. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | return $self->finish() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | if $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | $self->finish( | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 1, | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | $self->killall( 15, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 170 | $self->finish( | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 4, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $self->killall( 9, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | $self->finish(10); | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 25 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | Parallel::Runner - An object to manage running things in parallel processes. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | There are several other modules to do this, you probably want one of them. This | ||||||||||||||||||||||||||||||||||||||||||
| 192 | module exists as a super specialised parallel task manager. You create the | ||||||||||||||||||||||||||||||||||||||||||
| 193 | object with a process limit and callbacks for what to do while waiting for a | ||||||||||||||||||||||||||||||||||||||||||
| 194 | free process slot, as well as a callback for what a process should do just | ||||||||||||||||||||||||||||||||||||||||||
| 195 | before exiting. | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | You must explicitly call $runner->finish() when you are done. If the runner is | ||||||||||||||||||||||||||||||||||||||||||
| 198 | destroyed before it's children are finished a warning will be generated and | ||||||||||||||||||||||||||||||||||||||||||
| 199 | your child processes will be killed, by force if necessary. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | If you specify a maximum of 1 then no forking will occur, and run() will block | ||||||||||||||||||||||||||||||||||||||||||
| 202 | until the coderef returns. You can force a fork by providing a boolean true | ||||||||||||||||||||||||||||||||||||||||||
| 203 | value as the second argument to run(), this will force the runner to fork | ||||||||||||||||||||||||||||||||||||||||||
| 204 | before running the coderef, however run() will still block until it the child | ||||||||||||||||||||||||||||||||||||||||||
| 205 | exits. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 210 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 211 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 212 | use Parallel::Runner; | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | my $runner = Parallel::Runner->new(4); | ||||||||||||||||||||||||||||||||||||||||||
| 215 | $runner->run( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 216 | $runner->run( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 217 | $runner->run( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 218 | $runner->run( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # This will block until one of the previous 4 finishes | ||||||||||||||||||||||||||||||||||||||||||
| 221 | $runner->run( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | # Do not forget this. | ||||||||||||||||||||||||||||||||||||||||||
| 224 | $runner->finish; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | =item $runner = $class->new( $max, $accessor => $value, ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | Create a new instance of Parallel::Runner. $accessor can be anything listed | ||||||||||||||||||||||||||||||||||||||||||
| 233 | under the ACCESSORS section. $max should be the maximum number of processes | ||||||||||||||||||||||||||||||||||||||||||
| 234 | allowed, defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | =head1 ACCESSORS | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | These are simple accessors, providing an argument sets the accessor to that | ||||||||||||||||||||||||||||||||||||||||||
| 241 | argument, no argument it simply returns the current value. | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | =item $val = $runner->data_callback( \&callback ) | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | If this is specified than IPC will be automatically enabled, and the final | ||||||||||||||||||||||||||||||||||||||||||
| 248 | return from each process will be passed into this handler in the main process. | ||||||||||||||||||||||||||||||||||||||||||
| 249 | Due to the way IPC works only strings/numerical data is passed, if you need to | ||||||||||||||||||||||||||||||||||||||||||
| 250 | pass a ref you will need to serialize it yourself before returning it, followed | ||||||||||||||||||||||||||||||||||||||||||
| 251 | by deserializing it in your callback. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # Place to put the accumulated data | ||||||||||||||||||||||||||||||||||||||||||
| 256 | my @accum_data; | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | # Create the runner with a callback that pushes the data onto our array. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | $runner = $CLASS->new( 2, | ||||||||||||||||||||||||||||||||||||||||||
| 260 | data_callback => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | my ($data) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 262 | push @accum_data => $data; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | # 4 processes that return data | ||||||||||||||||||||||||||||||||||||||||||
| 267 | $runner->run( sub { return "foo" }); | ||||||||||||||||||||||||||||||||||||||||||
| 268 | $runner->run( sub { return "bar" }); | ||||||||||||||||||||||||||||||||||||||||||
| 269 | $runner->run( sub { return "baz" }); | ||||||||||||||||||||||||||||||||||||||||||
| 270 | $runner->run( sub { return "bat" }); | ||||||||||||||||||||||||||||||||||||||||||
| 271 | $runner->finish; | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | # Verify the data (order is not predictable) | ||||||||||||||||||||||||||||||||||||||||||
| 274 | is_deeply( | ||||||||||||||||||||||||||||||||||||||||||
| 275 | [ sort @accum_data ], | ||||||||||||||||||||||||||||||||||||||||||
| 276 | [ sort qw/foo bar baz bat/ ], | ||||||||||||||||||||||||||||||||||||||||||
| 277 | "Got all data returned by subprocesses" | ||||||||||||||||||||||||||||||||||||||||||
| 278 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | =item $val = $runner->exit_callback( \&callback ) | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | Codref to call just before a child exits (called within child) | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | =item $val = $runner->iteration_delay( $float ); | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | How long to wait per iterate if nothing has changed. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | =item $val = $runner->iteration_callback( $newval ) | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | Coderef to call multiple times in a loop while run() is blocking waiting for a | ||||||||||||||||||||||||||||||||||||||||||
| 291 | process slot. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =item $val = $runner->reap_callback( $newval ) | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | Codref to call whenever a pid is reaped using waitpid. The callback sub will be | ||||||||||||||||||||||||||||||||||||||||||
| 296 | passed 3 values The first is the exit status of the child process. The second | ||||||||||||||||||||||||||||||||||||||||||
| 297 | is the pid of the child process. The third used to be the return of waitpid, | ||||||||||||||||||||||||||||||||||||||||||
| 298 | but this is deprecated as L<Child> is now used and throws an exception when | ||||||||||||||||||||||||||||||||||||||||||
| 299 | waitpid is not what it should be. The third is simply the pid of the child | ||||||||||||||||||||||||||||||||||||||||||
| 300 | process again. The final argument is the child process object itself. | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | $runner->reap_callback( sub { | ||||||||||||||||||||||||||||||||||||||||||
| 303 | my ( $status, $pid, $pid_again, $proc ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | # Status as returned from system, so 0 is good, 1+ is bad. | ||||||||||||||||||||||||||||||||||||||||||
| 306 | die "Child $pid did not exit 0" | ||||||||||||||||||||||||||||||||||||||||||
| 307 | if $status; | ||||||||||||||||||||||||||||||||||||||||||
| 308 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | =item @children = $runner->children( @append ) | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | Returns a list of L<Child::Link::Proc> objects. | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | =item $val = $runner->pid() | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | pid of the parent process | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | =item $val = $runner->max( $newval ) | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | Maximum number of children | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | =head1 OBJECT METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | =item run( $code ) | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =item run( $code, $force_fork ) | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | Run the specified code in a child process. Blocks if no free slots are | ||||||||||||||||||||||||||||||||||||||||||
| 333 | available. Force fork can be used to force a fork when max is 1, however it | ||||||||||||||||||||||||||||||||||||||||||
| 334 | will still block until the child exits. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | =item finish() | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | =item finish( $timeout ) | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =item finish( $timeout, $timeoutcallback ) | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | Wait for all children to finish, then clean up after them. If a timeout is | ||||||||||||||||||||||||||||||||||||||||||
| 343 | specified it will return after the timeout regardless of wether or not children | ||||||||||||||||||||||||||||||||||||||||||
| 344 | have all exited. If there is a timeout call back then that code will be run | ||||||||||||||||||||||||||||||||||||||||||
| 345 | upon timeout just before the method returns. | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | NOTE: DO NOT LET YOUR RUNNER BE DESTROYED BEFORE FINISH COMPLETES WITHOUT A | ||||||||||||||||||||||||||||||||||||||||||
| 348 | TIMEOUT. | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | the runner will kill all children, possibly with force if your runner is | ||||||||||||||||||||||||||||||||||||||||||
| 351 | destroyed with children still running, or not waited on. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | =item killall( $sig ) | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | Send all children the specified kill signal. | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | =item DESTROY() | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | Automagically called when the object is destroyed. If called while children are | ||||||||||||||||||||||||||||||||||||||||||
| 360 | running it will forcefully clean up after you as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 1) Sends an ugly warning. | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 2) Will first give all your children 1 second to complete. | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | Windows) Strawberry fails with processes, so on windows DESTROY will wait as | ||||||||||||||||||||||||||||||||||||||||||
| 367 | long as needed, possibly forever. | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | 3) Sends kill signal 15 to all children then waits up to 4 seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | 4) Sends kill signal 9 to any remaining children then waits up to 10 seconds | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 5) Gives up and returns | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | =head1 FENNEC PROJECT | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | This module is part of the Fennec project. See L<Fennec> for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 380 | Fennec is a project to develop an extendable and powerful testing framework. | ||||||||||||||||||||||||||||||||||||||||||
| 381 | Together the tools that make up the Fennec framework provide a potent testing | ||||||||||||||||||||||||||||||||||||||||||
| 382 | environment. | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | The tools provided by Fennec are also useful on their own. Sometimes a tool | ||||||||||||||||||||||||||||||||||||||||||
| 385 | created for Fennec is useful outside the greater framework. Such tools are | ||||||||||||||||||||||||||||||||||||||||||
| 386 | turned into their own projects. This is one such project. | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | =item L<Fennec> - The core framework | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | The primary Fennec project that ties them all together. | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | Chad Granum L<exodist7@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | Copyright (C) 2010 Chad Granum | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | Parallel-Runner is free software; Standard perl licence. | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | Parallel-Runner is distributed in the hope that it will be useful, but WITHOUT | ||||||||||||||||||||||||||||||||||||||||||
| 407 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||||||||||||||||||||||||||||||||||||||||||
| 408 | FOR A PARTICULAR PURPOSE. See the license for more details. | ||||||||||||||||||||||||||||||||||||||||||