blib/lib/Parallel/ForkManager.pm

filter: Statementstmt 92.0% 162 / 176 Branchbran 80.5% 58 / 72 Conditioncond 57.5% 19 / 33 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 92.8% 39 / 42 Podpod 68.1% 15 / 22 totaltotal 82.1% 299 / 364 CC 50 SCAR 12.9
CC
50
Cov
85%
start
7.4
wait_one_child
7.4
finish
6.0
CRAP 3.6
↑ Summary blib/lib/Parallel/ForkManager/Child.pm »
1 package Parallel::ForkManager;
2 120 our $AUTHORITY = 'cpan:DLUX';
3 # ABSTRACT: A simple parallel processing fork manager
4 120 $Parallel::ForkManager::VERSION = '2.04';
5 120 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 120 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 120 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 120 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 120 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 120 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 120 ▶use Parallel::ForkManager::Child;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 120 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 120 ▶use Moo 1.001000;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 120 has max_proc => (
19 is => 'ro',
20 required => 1,
21 writer => 'set_max_procs',
22 );
23  
24 has processes => (
25 is => 'ro',
26 250 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 120 );
28  
29 has parent_pid => (
30 is => 'ro',
31 250 ▶ default => sub { $$ },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 120 );
33  
34 has auto_cleanup => (
35 is => 'rw',
36 250 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 120 );
38  
39 has waitpid_blocking_sleep => (
40 is =>'ro',
41 writer => 'set_waitpid_blocking_sleep',
42 250 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 120 );
44  
45 has tempdir => (
46 is => 'ro',
47 247 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 247 File::Temp::tempdir(CLEANUP => 0);
49 },
50 3 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
51 3 my( $self, $dir ) = @_;
52  
53 3 ▶ die qq|Temporary directory "$dir" doesn't exist or is not a directory.|
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 3 3
54 unless -d $dir;
55  
56 3 $self->auto_cleanup(0);
57 },
58 120 );
59  
60 120 has child_role => (
61 is => 'ro',
62 default => 'Parallel::ForkManager::Child',
63 );
64  
65 0 ▶sub is_child { 0 }
Subroutine is_child: not called CC=1 SCAR=6.9
Pod documented
66 0 ▶sub is_parent { 1 }
Subroutine is_parent: not called CC=1 SCAR=6.9
Pod documented
67  
68  
69 250 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=3 SCAR=11.2
Pod undocumented
70 250 my ( undef, @args ) = @_;
71 250 my %args;
72  
73 250 ▶ if ( $args[0] =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] =~ /^\d+$/) { } 250 0 250
74 250 $args{max_proc} = shift @args;
75 250 ▶ $args{tempdir} = shift @args if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 3 247 250
76 }
77 else {
78 0 %args = @args;
79 }
80  
81 250 return \%args;
82 }
83  
84 3442 ▶sub start {
Subroutine start: called CC=7 SCAR=20.0
Pod documented
85 3442 my ($s,$identification)=@_;
86  
87 3442 ▶ while ($s->{max_proc} && ( keys %{ $s->{processes} } ) >= $s->{max_proc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"max_proc"} and keys %{$$s{"processes"};} >= $s->{"max_proc"}) 2937 3442 6379
Condition66%2/3
$s->{"max_proc"} and keys %{$$s{"processes"};} >= $s->{"max_proc"}
!ll&&!rl&&r
034422937
MC/DC50%1/2
$s->{"max_proc"} and keys %{$$s{"processes"};} >= $s->{"max_proc"}
$s->{"max_proc"} keys %{$$s{"processes"};} >= $s->{"max_proc"}
Truth table66%2/3
A and B
A= $s->{"max_proc"}B= keys %{$$s{"processes"};} >= $s->{"max_proc"}
ABresult
0X0
100
111
88 2937 $s->on_wait;
89 2937 ▶ $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'on_wait_period'} ? : 37 2900 2937
90 };
91 3442 $s->wait_children;
92 3442 ▶ if ($s->{max_proc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"max_proc"}) 3442 0 3442
93 3442 my $pid=fork();
94 3442 ▶ die "Cannot fork: $!" if !defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 3442 3442
95 3442 ▶ if ($pid) { # in parent
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 3333 109 3442
96 3333 $s->{processes}->{$pid}=$identification;
97 3333 $s->on_start($pid,$identification);
98 } else {
99 109 Role::Tiny->apply_roles_to_object( $s, $s->child_role );
100 }
101 3442 return $pid;
102 }
103  
104 # non-forking mode
105 0 $s->{processes}->{$$}=$identification;
106 0 $s->on_start($$,$identification);
107 0 return 0; # Simulating the child which returns 0
108 }
109  
110 20 ▶sub start_child {
Subroutine start_child: called CC=2 SCAR=6.9
Pod documented
111 20 my $self = shift;
112 20 my $sub = pop;
113 20 my $identification = shift;
114  
115 20 ▶ $self->start( $identification ) # in the parent
Branch: 1 branch
BranchTrueFalseTotal
$self->start($identification) or $self->finish(0, &$sub()) 15 5 20
116 # ... or the child
117 or $self->finish( 0, $sub->() );
118 }
119  
120  
121 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
Pod documented
122 0 my ($s, $x, $r)=@_;
123  
124 0 ▶ if ($s->{max_proc} == 0) { # nofork
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"max_proc"} == 0) 0 0 0
125 0 $s->on_finish($$, $x ,$s->{processes}->{$$}, 0, 0, $r);
126 0 delete $s->{processes}->{$$};
127 }
128  
129 0 return 0;
130 }
131  
132 3442 ▶sub wait_children {
Subroutine wait_children: called CC=5 SCAR=16.4
Pod undocumented
133 3442 my ($s)=@_;
134  
135 3442 ▶ return if !keys %{$s->{processes}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$s{"processes"};} 2567 875 3442
136 875 my $kid;
137 875 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $kid and $kid > 0 || $kid < -1 and do { do { $kid = $s->wait_one_child(&WNOHANG) } } 151 875 1026
Condition50%3/6
defined $kid and $kid > 0 || $kid < -1
!ll&&!rl&&r
8750151
$kid > 0 or $kid < -1
l!l&&r!l&&!r
15100
MC/DC33%1/3
defined $kid and $kid > 0 || $kid < -1
defined $kid $kid > 0 $kid < -1
Truth table50%2/4
A and B or C
A= defined $kidB= $kid > 0C= $kid < -1
ABCresult
0XX0
1000
11X1
1011
138 1026 $kid = $s->wait_one_child(&WNOHANG);
139 } while defined $kid and ( $kid > 0 or $kid < -1 ); # AS 5.6/Win32 returns negative PIDs
140 };
141  
142 120 *wait_childs=*wait_children; # compatibility
143 120 *reap_finished_children=*wait_children; # behavioral synonym for clarity
144  
145 # TODO document the method
146 3248 ▶sub retrieve {
Subroutine retrieve: called CC=4 SCAR=14.4
Pod documented
147 3248 my( $self, $kid ) = @_;
148  
149 3248 my $retrieved = undef;
150  
151 3248 my $storable_tempfile = File::Spec->catfile($self->{tempdir}, 'Parallel-ForkManager-' . $self->{parent_pid} . '-' . $kid . '.txt');
152  
153 3248 ▶ if (-e $storable_tempfile) { # child has option of not storing anything, so we need to see if it did or not
Branch: 1 branch
BranchTrueFalseTotal
if (-e $storable_tempfile) 48 3200 3248
154 48 $retrieved = eval { Storable::retrieve($storable_tempfile) };
155  
156 # handle Storables errors
157 48 ▶ if (not $retrieved or $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $retrieved or $@) 0 48 48
Condition33%1/3
not $retrieved or $@
l!l&&r!l&&!r
0048
MC/DC0%0/2
not $retrieved or $@
not $retrieved $@
Truth table33%1/3
not(A) or B
A= not $retrievedB= $@
ABresult
1X1
011
000
158 0 warn(qq|The storable module was unable to retrieve the child's data structure from the temporary file "$storable_tempfile": | . join(', ', $@));
159 }
160  
161 # clean up after ourselves
162 48 unlink $storable_tempfile;
163 }
164  
165 3248 return $retrieved;
166 }
167  
168 108 ▶sub store {
Subroutine store: called CC=4 SCAR=14.8
Pod documented
169 108 my( $self, $data ) = @_;
170  
171 108 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 92 16 108
172  
173 16 my $storable_tempfile = File::Spec->catfile($self->{tempdir}, 'Parallel-ForkManager-' . $self->{parent_pid} . '-' . $$ . '.txt');
174 16 my $stored = eval { return Storable::store($data, $storable_tempfile); };
175  
176 # handle Storables errors, IE logcarp or carp returning undef, or die (via logcroak or croak)
177 16 ▶ if (not $stored or $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $stored or $@) 0 16 16
Condition33%1/3
not $stored or $@
l!l&&r!l&&!r
0016
MC/DC0%0/2
not $stored or $@
not $stored $@
Truth table33%1/3
not(A) or B
A= not $storedB= $@
ABresult
1X1
011
000
178 0 warn(qq|The storable module was unable to store the child's data structure to the temp file "$storable_tempfile": | . join(', ', $@));
179 }
180 }
181  
182  
183  
184 4175 ▶sub wait_one_child {
Subroutine wait_one_child: called CC=7 SCAR=20.0
Pod undocumented
185 4175 my ($s,$flag)=@_;
186  
187 4175 my $kid;
188 4175 while (1) {
189 4175 ▶ $kid = $s->_waitpid($flag||=0);
Condition100%2/2
$flag ||= 0
l!l
10883087
MC/DC100%1/1
$flag ||= 0
$flag
Truth table100%2/2
A
A= $flag
Aresult
11
00
190  
191 4175 ▶ last unless defined $kid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kid 927 3248 4175
192  
193 3248 ▶ last if $kid == 0 || $kid == -1; # AS 5.6/Win32 returns negative PIDs
Branch: 1 branch
BranchTrueFalseTotal
if $kid == 0 or $kid == -1 0 3248 3248
Condition33%1/3
$kid == 0 or $kid == -1
l!l&&r!l&&!r
003248
MC/DC0%0/2
$kid == 0 or $kid == -1
$kid == 0 $kid == -1
Truth table33%1/3
A or B
A= $kid == 0B= $kid == -1
ABresult
1X1
011
000
194 3248 ▶ redo if !exists $s->{processes}->{$kid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s->{"processes"}{$kid} 0 3248 3248
195 3248 my $id = delete $s->{processes}->{$kid};
196  
197 # retrieve child data structure, if any
198 3248 my $retrieved = $s->retrieve($kid);
199  
200 3248 ▶ $s->on_finish( $kid, $? >> 8 , $id, $? & 0x7f, $? & 0x80 ? 1 : 0, $retrieved);
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 3248 3248
201 3248 last;
202 }
203 4175 $kid;
204 };
205  
206 138 ▶sub wait_all_children {
Subroutine wait_all_children: called CC=3 SCAR=11.0
Pod documented
207 138 my ($s)=@_;
208  
209 138 ▶ while (keys %{ $s->{processes} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$s{"processes"};}) 208 138 346
210 208 $s->on_wait;
211 208 ▶ $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'on_wait_period'} ? : 25 183 208
212 };
213 }
214  
215 120 *wait_all_childs=*wait_all_children; # compatibility;
216  
217 5 ▶sub max_procs { $_[0]->max_proc }
Subroutine max_procs: called CC=1 SCAR=0.0
Pod documented
218  
219 4413 ▶sub running_procs {
Subroutine running_procs: called CC=1 SCAR=0.0
Pod documented
220 4413 my $self = shift;
221  
222 4413 my @pids = keys %{ $self->{processes} };
223 4413 return @pids;
224 }
225  
226 1 ▶sub running_procs_with_identifiers {
Subroutine running_procs_with_identifiers: called CC=1 SCAR=0.0
Pod documented
227 1 my $self = shift;
228 1 return %{ $self->{processes} };
229 }
230  
231 1 ▶sub wait_for_available_procs {
Subroutine wait_for_available_procs: called CC=4 SCAR=14.6
Pod documented
232 1 my( $self, $nbr ) = @_;
233 1 ▶ $nbr ||= 1;
Condition50%1/2
$nbr ||= 1
l!l
10
MC/DC0%0/1
$nbr ||= 1
$nbr
Truth table50%1/2
A
A= $nbr
Aresult
11
00
234  
235 0 ▶ croak "number of processes '$nbr' higher than the max number of processes (@{[ $self->max_procs ]})"
Branch: 1 branch
BranchTrueFalseTotal
if $nbr > $self->max_procs 0 1 1
236 if $nbr > $self->max_procs;
237  
238 1 ▶ $self->wait_one_child until $self->max_procs - $self->running_procs >= $nbr;
Branch: 1 branch
BranchTrueFalseTotal
$self->max_procs - $self->running_procs >= $nbr or do { $self->wait_one_child } 1 2 3
239 }
240  
241 36 ▶sub run_on_finish {
Subroutine run_on_finish: called CC=2 SCAR=7.2
Pod documented
242 36 my ($s,$code,$pid)=@_;
243  
244 36 ▶ $s->{on_finish}->{$pid || 0}=$code;
Condition50%1/2
$pid || 0
l!l
036
MC/DC0%0/1
$pid || 0
$pid
Truth table50%1/2
A
A= $pid
Aresult
11
00
245 }
246  
247 3248 ▶sub on_finish {
Subroutine on_finish: called CC=3 SCAR=11.0
Pod undocumented
248 3248 my ($s,$pid,@par)=@_;
249  
250 3248 ▶ my $code=$s->{on_finish}->{$pid} || $s->{on_finish}->{0} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $s->{"on_finish"}{$pid} || $s->{"on_finish"}{0} or return 0 60 3188 3248
Condition66%2/3
$s->{'on_finish'}{$pid} || $s->{'on_finish'}{'0'}
l!l&&r!l&&!r
0603188
MC/DC50%1/2
$s->{'on_finish'}{$pid} || $s->{'on_finish'}{'0'}
$s->{'on_finish'}{$pid} $s->{'on_finish'}{'0'}
Truth table66%2/3
A || B
A= $s->{'on_finish'}{$pid}B= $s->{'on_finish'}{'0'}
ABresult
1X1
011
000
251 60 $code->($pid,@par);
252 };
253  
254 7 ▶sub run_on_wait {
Subroutine run_on_wait: called CC=1 SCAR=0.0
Pod documented
255 7 my ($s,$code, $period)=@_;
256  
257 7 $s->{on_wait}=$code;
258 7 $s->{on_wait_period} = $period;
259 }
260  
261 3145 ▶sub on_wait {
Subroutine on_wait: called CC=4 SCAR=14.0
Pod undocumented
262 3145 my ($s)=@_;
263  
264 3145 ▶ if(ref($s->{on_wait}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s->{"on_wait"} eq "CODE") 62 3083 3145
265 62 $s->{on_wait}->();
266 62 ▶ if (defined $s->{on_wait_period}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"on_wait_period"}) 62 0 62
267 62 ▶ local $SIG{CHLD} = sub { } if ! defined $SIG{CHLD};
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SIG{"CHLD"} 62 0 62
268 select undef, undef, undef, $s->{on_wait_period}
269 62 };
270 };
271 };
272  
273 13 ▶sub run_on_start {
Subroutine run_on_start: called CC=1 SCAR=0.0
Pod documented
274 13 my ($s,$code)=@_;
275  
276 13 $s->{on_start}=$code;
277 }
278  
279 3333 ▶sub on_start {
Subroutine on_start: called CC=2 SCAR=6.9
Pod undocumented
280 3333 my ($s,@par)=@_;
281  
282 3333 ▶ $s->{on_start}->(@par) if ref($s->{on_start}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $s->{"on_start"} eq "CODE" 27 3306 3333
283 };
284  
285 4175 ▶sub _waitpid { # Call waitpid() in the standard Unix fashion.
Subroutine _waitpid: called CC=2 SCAR=6.9
286 4175 my( $self, $flag ) = @_;
287  
288 4175 ▶ return $flag ? $self->_waitpid_non_blocking : $self->_waitpid_blocking;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 1088 3087 4175
289 }
290  
291 3297 ▶sub _waitpid_non_blocking {
Subroutine _waitpid_non_blocking: called CC=4 SCAR=13.9
292 3297 my $self = shift;
293  
294 3297 for my $pid ( $self->running_procs ) {
295 4165 ▶ my $p = waitpid $pid, &WNOHANG or next;
Branch: 1 branch
BranchTrueFalseTotal
my $p = waitpid($pid, &WNOHANG) or next 1267 2898 4165
296  
297 1267 ▶ return $pid if $p != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p != -1 1265 2 1267
298  
299 2 warn "child process '$pid' disappeared. A call to `waitpid` outside of Parallel::ForkManager might have reaped it.\n";
300 # it's gone. let's clean the process entry
301 2 delete $self->{processes}{$pid};
302 }
303  
304 2032 return;
305 }
306  
307 3087 ▶sub _waitpid_blocking {
Subroutine _waitpid_blocking: called CC=4 SCAR=13.9
308 3087 my $self = shift;
309  
310 # pseudo-blocking
311 3087 ▶ if( my $sleep_period = $self->{waitpid_blocking_sleep} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sleep_period = $self->{"waitpid_blocking_sleep"}) 1104 1983 3087
312 1104 while() {
313 2209 my $pid = $self->_waitpid_non_blocking;
314  
315 2209 ▶ return $pid if defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid 1102 1107 2209
316  
317 1107 ▶ return unless $self->running_procs;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->running_procs 2 1105 1107
318  
319 1105 select undef, undef, undef, $sleep_period;
320 }
321 }
322  
323 1983 return waitpid -1, 0;
324 }
325  
326 250 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=4 SCAR=13.9
Pod undocumented
327 250 my $self = shift;
328  
329 120 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
330  
331 File::Path::remove_tree($self->{tempdir})
332 250 ▶ if $self->{auto_cleanup} and $self->{parent_pid} == $$ and -d $self->{tempdir};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_cleanup"} and $self->{"parent_pid"} == $$ and -d $self->{"tempdir"} 139 111 250
Condition83%5/6
$self->{"auto_cleanup"} and $self->{"parent_pid"} == $$ and -d $self->{"tempdir"}
!ll&&!rl&&r
1110139
$self->{"auto_cleanup"} and $self->{"parent_pid"} == $$
!ll&&!rl&&r
3108139
MC/DC66%2/3
$self->{"auto_cleanup"} and $self->{"parent_pid"} == $$ and -d $self->{"tempdir"}
$self->{"auto_cleanup"} $self->{"parent_pid"} == $$ -d $self->{"tempdir"}
Truth table75%3/4
A and B and C
A= $self->{"auto_cleanup"}B= $self->{"parent_pid"} == $$C= -d $self->{"tempdir"}
ABCresult
0XX0
10X0
1100
1111
333 }
334  
335 120 1;
336  
337 __END__
↑ Summary blib/lib/Parallel/ForkManager/Child.pm »