blib/lib/Parallel/ForkManager.pm

filter: Statementstmt 91.9% 159 / 173 Branchbran 80.5% 58 / 72 Conditioncond 57.5% 19 / 33 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 92.6% 38 / 41 Podpod 66.6% 14 / 21 totaltotal 81.8% 294 / 359 SCAR 40.7
CC
50
Cov
85%
start
7.4
wait_one_child
7.4
finish
6.0
CRAP 58.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.02';
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;
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 default => sub {
48 247 ▶ File::Temp::tempdir(CLEANUP => 0);
Subroutine __ANON__: called CC=1 SCAR=0.0
49 },
50 trigger => sub {
51 3 ▶ my( $self, $dir ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
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 sub BUILDARGS {
70 250 ▶ my ( undef, @args ) = @_;
Subroutine BUILDARGS: called CC=3 SCAR=11.2
Pod undocumented
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 sub start {
85 3442 ▶ my ($s,$identification)=@_;
Subroutine start: called CC=7 SCAR=20.0
Pod documented
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"}) 2932 3442 6374
Condition66%2/3
$s->{"max_proc"} and keys %{$$s{"processes"};} >= $s->{"max_proc"}
!ll&&!rl&&r
034422932
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 2932 $s->on_wait;
89 2932 ▶ $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'on_wait_period'} ? : 32 2900 2932
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 sub start_child {
111 20 ▶ my $self = shift;
Subroutine start_child: called CC=2 SCAR=6.9
Pod documented
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()) 5 15 20
116 # ... or the child
117 or $self->finish( 0, $sub->() );
118 }
119  
120  
121 sub finish {
122 0 ▶ my ($s, $x, $r)=@_;
Subroutine finish: not called CC=2 SCAR=17.9
Pod documented
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 sub wait_children {
133 3442 ▶ my ($s)=@_;
Subroutine wait_children: called CC=5 SCAR=16.4
Pod undocumented
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) } } 153 875 1028
Condition50%3/6
defined $kid and $kid > 0 || $kid < -1
!ll&&!rl&&r
8750153
$kid > 0 or $kid < -1
l!l&&r!l&&!r
15300
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 1028 $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 sub retrieve {
147 3249 ▶ my( $self, $kid ) = @_;
Subroutine retrieve: called CC=4 SCAR=14.4
Pod documented
148  
149 3249 my $retrieved = undef;
150  
151 3249 my $storable_tempfile = File::Spec->catfile($self->{tempdir}, 'Parallel-ForkManager-' . $self->{parent_pid} . '-' . $kid . '.txt');
152  
153 3249 ▶ 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) 49 3200 3249
154 49 $retrieved = eval { Storable::retrieve($storable_tempfile) };
155  
156 # handle Storables errors
157 49 ▶ if (not $retrieved or $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $retrieved or $@) 0 49 49
Condition33%1/3
not $retrieved or $@
l!l&&r!l&&!r
0049
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 49 unlink $storable_tempfile;
163 }
164  
165 3249 return $retrieved;
166 }
167  
168 sub store {
169 108 ▶ my( $self, $data ) = @_;
Subroutine store: called CC=4 SCAR=14.8
Pod documented
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 sub wait_one_child {
185 4171 ▶ my ($s,$flag)=@_;
Subroutine wait_one_child: called CC=7 SCAR=20.0
Pod undocumented
186  
187 4171 my $kid;
188 4171 while (1) {
189 4171 ▶ $kid = $s->_waitpid($flag||=0);
Condition100%2/2
$flag ||= 0
l!l
10853086
MC/DC100%1/1
$flag ||= 0
$flag
Truth table100%2/2
A
A= $flag
Aresult
11
00
190  
191 4171 ▶ last unless defined $kid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kid 922 3249 4171
192  
193 3249 ▶ last if $kid == 0 || $kid == -1; # AS 5.6/Win32 returns negative PIDs
Branch: 1 branch
BranchTrueFalseTotal
if $kid == 0 or $kid == -1 0 3249 3249
Condition33%1/3
$kid == 0 or $kid == -1
l!l&&r!l&&!r
003249
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 3249 ▶ redo if !exists $s->{processes}->{$kid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s->{"processes"}{$kid} 0 3249 3249
195 3249 my $id = delete $s->{processes}->{$kid};
196  
197 # retrieve child data structure, if any
198 3249 my $retrieved = $s->retrieve($kid);
199  
200 3249 ▶ $s->on_finish( $kid, $? >> 8 , $id, $? & 0x7f, $? & 0x80 ? 1 : 0, $retrieved);
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 3249 3249
201 3249 last;
202 }
203 4171 $kid;
204 };
205  
206 sub wait_all_children {
207 138 ▶ my ($s)=@_;
Subroutine wait_all_children: called CC=3 SCAR=11.0
Pod documented
208  
209 138 ▶ while (keys %{ $s->{processes} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$s{"processes"};}) 207 138 345
210 207 $s->on_wait;
211 207 ▶ $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'on_wait_period'} ? : 25 182 207
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 sub running_procs {
220 4407 ▶ my $self = shift;
Subroutine running_procs: called CC=1 SCAR=0.0
Pod documented
221  
222 4407 my @pids = keys %{ $self->{processes} };
223 4407 return @pids;
224 }
225  
226 sub wait_for_available_procs {
227 1 ▶ my( $self, $nbr ) = @_;
Subroutine wait_for_available_procs: called CC=4 SCAR=14.6
Pod documented
228 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
229  
230 1 ▶ 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
231 if $nbr > $self->max_procs;
232  
233 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 } 2 1 3
234 }
235  
236 sub run_on_finish {
237 36 ▶ my ($s,$code,$pid)=@_;
Subroutine run_on_finish: called CC=2 SCAR=7.2
Pod documented
238  
239 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
240 }
241  
242 sub on_finish {
243 3249 ▶ my ($s,$pid,@par)=@_;
Subroutine on_finish: called CC=3 SCAR=11.0
Pod undocumented
244  
245 3249 ▶ 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 3188 61 3249
Condition66%2/3
$s->{'on_finish'}{$pid} || $s->{'on_finish'}{'0'}
l!l&&r!l&&!r
0613188
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
246 61 $code->($pid,@par);
247 };
248  
249 sub run_on_wait {
250 7 ▶ my ($s,$code, $period)=@_;
Subroutine run_on_wait: called CC=1 SCAR=0.0
Pod documented
251  
252 7 $s->{on_wait}=$code;
253 7 $s->{on_wait_period} = $period;
254 }
255  
256 sub on_wait {
257 3139 ▶ my ($s)=@_;
Subroutine on_wait: called CC=4 SCAR=14.0
Pod undocumented
258  
259 3139 ▶ if(ref($s->{on_wait}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s->{"on_wait"} eq "CODE") 57 3082 3139
260 57 $s->{on_wait}->();
261 57 ▶ if (defined $s->{on_wait_period}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"on_wait_period"}) 57 0 57
262 57 ▶ local $SIG{CHLD} = sub { } if ! defined $SIG{CHLD};
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SIG{"CHLD"} 57 0 57
263 select undef, undef, undef, $s->{on_wait_period}
264 57 };
265 };
266 };
267  
268 sub run_on_start {
269 13 ▶ my ($s,$code)=@_;
Subroutine run_on_start: called CC=1 SCAR=0.0
Pod documented
270  
271 13 $s->{on_start}=$code;
272 }
273  
274 sub on_start {
275 3333 ▶ my ($s,@par)=@_;
Subroutine on_start: called CC=2 SCAR=6.9
Pod undocumented
276  
277 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
278 };
279  
280 sub _waitpid { # Call waitpid() in the standard Unix fashion.
281 4171 ▶ my( $self, $flag ) = @_;
Subroutine _waitpid: called CC=2 SCAR=6.9
282  
283 4171 ▶ return $flag ? $self->_waitpid_non_blocking : $self->_waitpid_blocking;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 1085 3086 4171
284 }
285  
286 sub _waitpid_non_blocking {
287 3292 ▶ my $self = shift;
Subroutine _waitpid_non_blocking: called CC=4 SCAR=13.9
288  
289 3292 for my $pid ( $self->running_procs ) {
290 4134 ▶ my $p = waitpid $pid, &WNOHANG or next;
Branch: 1 branch
BranchTrueFalseTotal
my $p = waitpid($pid, &WNOHANG) or next 2866 1268 4134
291  
292 1268 ▶ return $pid if $p != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p != -1 1266 2 1268
293  
294 2 warn "child process '$pid' disappeared. A call to `waitpid` outside of Parallel::ForkManager might have reaped it.\n";
295 # it's gone. let's clean the process entry
296 2 delete $self->{processes}{$pid};
297 }
298  
299 2026 return;
300 }
301  
302 sub _waitpid_blocking {
303 3086 ▶ my $self = shift;
Subroutine _waitpid_blocking: called CC=4 SCAR=13.9
304  
305 # pseudo-blocking
306 3086 ▶ if( my $sleep_period = $self->{waitpid_blocking_sleep} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sleep_period = $self->{"waitpid_blocking_sleep"}) 1103 1983 3086
307 1103 while() {
308 2207 my $pid = $self->_waitpid_non_blocking;
309  
310 2207 ▶ return $pid if defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid 1101 1106 2207
311  
312 1106 ▶ return unless $self->running_procs;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->running_procs 2 1104 1106
313  
314 1104 select undef, undef, undef, $sleep_period;
315 }
316 }
317  
318 1983 return waitpid -1, 0;
319 }
320  
321 sub DEMOLISH {
322 250 ▶ my $self = shift;
Subroutine DEMOLISH: called CC=4 SCAR=13.9
Pod undocumented
323  
324 120 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
325  
326 File::Path::remove_tree($self->{tempdir})
327 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
328 }
329  
330 120 1;
331  
332 __END__
↑ Summary blib/lib/Parallel/ForkManager/Child.pm »