File Coverage

blib/lib/Parallel/ForkManager.pm
Criterion Covered Total %
statement 141 155 90.9
branch 49 62 79.0
condition 19 33 57.5
subroutine 33 36 91.6
pod 15 22 68.1
total 257 308 83.4


line stmt bran cond sub pod time code
1             package Parallel::ForkManager;
2             our $AUTHORITY = 'cpan:DLUX';
3             # ABSTRACT: A simple parallel processing fork manager
4             $Parallel::ForkManager::VERSION = '2.04';
5 120     120   13238686 use POSIX ":sys_wait_h";
  120         1169982  
  120         743  
6 120     120   322426 use Storable ();
  120         562860  
  120         3813  
7 120     120   1082 use File::Spec;
  120         395  
  120         3510  
8 120     120   125204 use File::Temp ();
  120         3330799  
  120         6476  
9 120     120   1051 use File::Path ();
  120         227  
  120         2505  
10 120     120   553 use Carp;
  120         275  
  120         8121  
11              
12 120     120   67184 use Parallel::ForkManager::Child;
  120         498  
  120         5048  
13              
14 120     120   1038 use strict;
  120         514  
  120         5189  
15              
16 120     120   72576 use Moo 1.001000;
  120         403596  
  120         996  
17              
18             has max_proc => (
19             is => 'ro',
20             required => 1,
21             writer => 'set_max_procs',
22             );
23              
24             has processes => (
25             is => 'ro',
26             default => sub { {} },
27             );
28              
29             has parent_pid => (
30             is => 'ro',
31             default => sub { $$ },
32             );
33              
34             has auto_cleanup => (
35             is => 'rw',
36             default => sub { 1 },
37             );
38              
39             has waitpid_blocking_sleep => (
40             is =>'ro',
41             writer => 'set_waitpid_blocking_sleep',
42             default => sub { 1 },
43             );
44              
45             has tempdir => (
46             is => 'ro',
47             default => sub {
48             File::Temp::tempdir(CLEANUP => 0);
49             },
50             trigger => sub {
51             my( $self, $dir ) = @_;
52              
53             die qq|Temporary directory "$dir" doesn't exist or is not a directory.|
54             unless -d $dir;
55              
56             $self->auto_cleanup(0);
57             },
58             );
59              
60             has child_role => (
61             is => 'ro',
62             default => 'Parallel::ForkManager::Child',
63             );
64              
65 0     0 1 0 sub is_child { 0 }
66 0     0 1 0 sub is_parent { 1 }
67              
68              
69             sub BUILDARGS {
70 250     250 0 27206338 my ( undef, @args ) = @_;
71 250         714 my %args;
72              
73 250 50       2822 if ( $args[0] =~ /^\d+$/ ) {
74 250         1220 $args{max_proc} = shift @args;
75 250 100       1231 $args{tempdir} = shift @args if @args;
76             }
77             else {
78 0         0 %args = @args;
79             }
80              
81 250         6294 return \%args;
82             }
83              
84             sub start {
85 3442     3442 1 180771 my ($s,$identification)=@_;
86              
87 3442   66     137941 while ($s->{max_proc} && ( keys %{ $s->{processes} } ) >= $s->{max_proc}) {
  6347         131547  
88 2905         69434 $s->on_wait;
89 2905 100       70381 $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef);
90             };
91 3442         24998 $s->wait_children;
92 3442 50       15582 if ($s->{max_proc}) {
93 3442         8112753 my $pid=fork();
94 3442 50       126696 die "Cannot fork: $!" if !defined $pid;
95 3442 100       119074 if ($pid) { # in parent
96 3333         295397 $s->{processes}->{$pid}=$identification;
97 3333         166488 $s->on_start($pid,$identification);
98             } else {
99 109         33547 Role::Tiny->apply_roles_to_object( $s, $s->child_role );
100             }
101 3442         457056 return $pid;
102             }
103              
104             # non-forking mode
105 0         0 $s->{processes}->{$$}=$identification;
106 0         0 $s->on_start($$,$identification);
107 0         0 return 0; # Simulating the child which returns 0
108             }
109              
110             sub start_child {
111 20     20 1 877 my $self = shift;
112 20         49 my $sub = pop;
113 20         43 my $identification = shift;
114              
115 20 100       214 $self->start( $identification ) # in the parent
116             # ... or the child
117             or $self->finish( 0, $sub->() );
118             }
119              
120              
121             sub finish {
122 0     0 1 0 my ($s, $x, $r)=@_;
123              
124 0 0       0 if ($s->{max_proc} == 0) { # nofork
125 0         0 $s->on_finish($$, $x ,$s->{processes}->{$$}, 0, 0, $r);
126 0         0 delete $s->{processes}->{$$};
127             }
128              
129 0         0 return 0;
130             }
131              
132             sub wait_children {
133 3442     3442 0 8640 my ($s)=@_;
134              
135 3442 100       6295 return if !keys %{$s->{processes}};
  3442         15933  
136 875         4356 my $kid;
137 875   33     3878 do {
      66        
138 1059         27626 $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             *wait_childs=*wait_children; # compatibility
143             *reap_finished_children=*wait_children; # behavioral synonym for clarity
144              
145             # TODO document the method
146             sub retrieve {
147 3250     3250 1 13836 my( $self, $kid ) = @_;
148              
149 3250         8227 my $retrieved = undef;
150              
151 3250         157945 my $storable_tempfile = File::Spec->catfile($self->{tempdir}, 'Parallel-ForkManager-' . $self->{parent_pid} . '-' . $kid . '.txt');
152              
153 3250 100       229499 if (-e $storable_tempfile) { # child has option of not storing anything, so we need to see if it did or not
154 49         195 $retrieved = eval { Storable::retrieve($storable_tempfile) };
  49         520  
155              
156             # handle Storables errors
157 49 50 33     9953 if (not $retrieved or $@) {
158 0         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         7761 unlink $storable_tempfile;
163             }
164              
165 3250         17154 return $retrieved;
166             }
167              
168             sub store {
169 108     108 1 1604 my( $self, $data ) = @_;
170              
171 108 100       2428 return unless defined $data;
172              
173 16         2604 my $storable_tempfile = File::Spec->catfile($self->{tempdir}, 'Parallel-ForkManager-' . $self->{parent_pid} . '-' . $$ . '.txt');
174 16         259 my $stored = eval { return Storable::store($data, $storable_tempfile); };
  16         700  
175              
176             # handle Storables errors, IE logcarp or carp returning undef, or die (via logcroak or croak)
177 16 50 33     23049 if (not $stored or $@) {
178 0         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 4175     4175 0 2363684 my ($s,$flag)=@_;
186              
187 4175         8140 my $kid;
188 4175         8407 while (1) {
189 4175   100     171152 $kid = $s->_waitpid($flag||=0);
190              
191 4175 100       29942 last unless defined $kid;
192              
193 3250 50 33     56730 last if $kid == 0 || $kid == -1; # AS 5.6/Win32 returns negative PIDs
194 3250 50       19347 redo if !exists $s->{processes}->{$kid};
195 3250         18690 my $id = delete $s->{processes}->{$kid};
196              
197             # retrieve child data structure, if any
198 3250         33558 my $retrieved = $s->retrieve($kid);
199              
200 3250 50       76522 $s->on_finish( $kid, $? >> 8 , $id, $? & 0x7f, $? & 0x80 ? 1 : 0, $retrieved);
201 3250         9390 last;
202             }
203 4175         34527 $kid;
204             };
205              
206             sub wait_all_children {
207 138     138 1 2435700 my ($s)=@_;
208              
209 138         2450 while (keys %{ $s->{processes} }) {
  345         8090  
210 207         5719 $s->on_wait;
211 207 100       5613 $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef);
212             };
213             }
214              
215             *wait_all_childs=*wait_all_children; # compatibility;
216              
217 5     5 1 4354 sub max_procs { $_[0]->max_proc }
218              
219             sub running_procs {
220 4441     4441 1 15022 my $self = shift;
221              
222 4441         19589 my @pids = keys %{ $self->{processes} };
  4441         39119  
223 4441         56835 return @pids;
224             }
225              
226             sub running_procs_with_identifiers {
227 1     1 1 31 my $self = shift;
228 1         23 return %{ $self->{processes} };
  1         43  
229             }
230              
231             sub wait_for_available_procs {
232 1     1 1 2624 my( $self, $nbr ) = @_;
233 1   50     32 $nbr ||= 1;
234              
235 1 50       5 croak "number of processes '$nbr' higher than the max number of processes (@{[ $self->max_procs ]})"
  0         0  
236             if $nbr > $self->max_procs;
237              
238 1         16 $self->wait_one_child until $self->max_procs - $self->running_procs >= $nbr;
239             }
240              
241             sub run_on_finish {
242 36     36 1 389 my ($s,$code,$pid)=@_;
243              
244 36   50     475 $s->{on_finish}->{$pid || 0}=$code;
245             }
246              
247             sub on_finish {
248 3250     3250 0 16233 my ($s,$pid,@par)=@_;
249              
250 3250 100 66     44642 my $code=$s->{on_finish}->{$pid} || $s->{on_finish}->{0} or return 0;
251 61         643 $code->($pid,@par);
252             };
253              
254             sub run_on_wait {
255 7     7 1 21 my ($s,$code, $period)=@_;
256              
257 7         21 $s->{on_wait}=$code;
258 7         21 $s->{on_wait_period} = $period;
259             }
260              
261             sub on_wait {
262 3112     3112 0 29196 my ($s)=@_;
263              
264 3112 100       17467 if(ref($s->{on_wait}) eq 'CODE') {
265 60         482 $s->{on_wait}->();
266 60 50       262 if (defined $s->{on_wait_period}) {
267 60 50   12   2298 local $SIG{CHLD} = sub { } if ! defined $SIG{CHLD};
268             select undef, undef, undef, $s->{on_wait_period}
269 60         26844003 };
270             };
271             };
272              
273             sub run_on_start {
274 13     13 1 39 my ($s,$code)=@_;
275              
276 13         321 $s->{on_start}=$code;
277             }
278              
279             sub on_start {
280 3333     3333 0 113172 my ($s,@par)=@_;
281              
282 3333 100       31146 $s->{on_start}->(@par) if ref($s->{on_start}) eq 'CODE';
283             };
284              
285             sub _waitpid { # Call waitpid() in the standard Unix fashion.
286 4175     4175   13792 my( $self, $flag ) = @_;
287              
288 4175 100       54195 return $flag ? $self->_waitpid_non_blocking : $self->_waitpid_blocking;
289             }
290              
291             sub _waitpid_non_blocking {
292 3326     3326   14960 my $self = shift;
293              
294 3326         28563 for my $pid ( $self->running_procs ) {
295 4141 100       152573 my $p = waitpid $pid, &WNOHANG or next;
296              
297 1299 100       11777 return $pid if $p != -1;
298              
299 2         59 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         134 delete $self->{processes}{$pid};
302             }
303              
304 2029         8353 return;
305             }
306              
307             sub _waitpid_blocking {
308 3056     3056   40931 my $self = shift;
309              
310             # pseudo-blocking
311 3056 100       35909 if( my $sleep_period = $self->{waitpid_blocking_sleep} ) {
312 1103         15274 while() {
313 2207         25348 my $pid = $self->_waitpid_non_blocking;
314              
315 2207 100       18428 return $pid if defined $pid;
316              
317 1106 100       2938 return unless $self->running_procs;
318              
319 1104         1109479345 select undef, undef, undef, $sleep_period;
320             }
321             }
322              
323 1953         637137886 return waitpid -1, 0;
324             }
325              
326             sub DEMOLISH {
327 250     250 0 845011 my $self = shift;
328              
329 120     120   508011 no warnings 'uninitialized';
  120         455  
  120         17115  
330              
331             File::Path::remove_tree($self->{tempdir})
332 250 100 100     133719 if $self->{auto_cleanup} and $self->{parent_pid} == $$ and -d $self->{tempdir};
      66        
333             }
334              
335             1;
336              
337             __END__
338              
339             =pod
340              
341             =encoding UTF-8
342              
343             =head1 NAME
344              
345             Parallel::ForkManager - A simple parallel processing fork manager
346              
347             =head1 VERSION
348              
349             version 2.04
350              
351             =head1 SYNOPSIS
352              
353             use Parallel::ForkManager;
354              
355             my $pm = Parallel::ForkManager->new($MAX_PROCESSES);
356              
357             DATA_LOOP:
358             foreach my $data (@all_data) {
359             # Forks and returns the pid for the child:
360             my $pid = $pm->start and next DATA_LOOP;
361              
362             ... do some work with $data in the child process ...
363              
364             $pm->finish; # Terminates the child process
365             }
366              
367             =head1 DESCRIPTION
368              
369             This module is intended for use in operations that can be done in parallel
370             where the number of processes to be forked off should be limited. Typical
371             use is a downloader which will be retrieving hundreds/thousands of files.
372              
373             The code for a downloader would look something like this:
374              
375             use LWP::Simple;
376             use Parallel::ForkManager;
377              
378             ...
379              
380             my @links=(
381             ["http://www.foo.bar/rulez.data","rulez_data.txt"],
382             ["http://new.host/more_data.doc","more_data.doc"],
383             ...
384             );
385              
386             ...
387              
388             # Max 30 processes for parallel download
389             my $pm = Parallel::ForkManager->new(30);
390              
391             LINKS:
392             foreach my $linkarray (@links) {
393             $pm->start and next LINKS; # do the fork
394              
395             my ($link, $fn) = @$linkarray;
396             warn "Cannot get $fn from $link"
397             if getstore($link, $fn) != RC_OK;
398              
399             $pm->finish; # do the exit in the child process
400             }
401             $pm->wait_all_children;
402              
403             First you need to instantiate the ForkManager with the "new" constructor.
404             You must specify the maximum number of processes to be created. If you
405             specify 0, then NO fork will be done; this is good for debugging purposes.
406              
407             Next, use $pm->start to do the fork. $pm returns 0 for the child process,
408             and child pid for the parent process (see also L<perlfunc/fork>).
409             The "and next" skips the internal loop in the parent process. NOTE:
410             $pm->start dies if the fork fails.
411              
412             $pm->finish terminates the child process (assuming a fork was done in the
413             "start").
414              
415             NOTE: You cannot use $pm->start if you are already in the child process.
416             If you want to manage another set of subprocesses in the child process,
417             you must instantiate another Parallel::ForkManager object!
418              
419             =head1 METHODS
420              
421             The comment letter indicates where the method should be run. P for parent,
422             C for child.
423              
424             =over 5
425              
426             =item new $processes
427              
428             Instantiate a new Parallel::ForkManager object. You must specify the maximum
429             number of children to fork off. If you specify 0 (zero), then no children
430             will be forked. This is intended for debugging purposes.
431              
432             The optional second parameter, $tempdir, is only used if you want the
433             children to send back a reference to some data (see RETRIEVING DATASTRUCTURES
434             below). If not provided, it is set via a call to L<File::Temp>::tempdir().
435              
436             The new method will die if the temporary directory does not exist or it is not
437             a directory.
438              
439             Since version 2.00, the constructor can also be called in the typical Moo/Moose
440             fashion. I.e.
441              
442             my $fm = Parallel::ForkManager->new(
443             max_proc => 4,
444             tempdir => '...',
445             child_role => 'Parallel::ForkManager::CustomChild',
446             );
447              
448             =item child_role
449              
450             Returns the name of the role consumed by the ForkManager object in
451             child processes. Defaults to L<Parallel::ForkManager::Child> and can
452             be set to something else via the constructor.
453              
454             =item start [ $process_identifier ]
455              
456             This method does the fork. It returns the pid of the child process for
457             the parent, and 0 for the child process. If the $processes parameter
458             for the constructor is 0 then, assuming you're in the child process,
459             $pm->start simply returns 0.
460              
461             An optional $process_identifier can be provided to this method... It is used by
462             the "run_on_finish" callback (see CALLBACKS) for identifying the finished
463             process.
464              
465             =item start_child [ $process_identifier, ] \&callback
466              
467             Like C<start>, but will run the C<&callback> as the child. If the callback returns anything,
468             it'll be passed as the data to transmit back to the parent process via C<finish()>.
469              
470             =item finish [ $exit_code [, $data_structure_reference] ]
471              
472             Closes the child process by exiting and accepts an optional exit code
473             (default exit code is 0) which can be retrieved in the parent via callback.
474             If the second optional parameter is provided, the child attempts to send
475             its contents back to the parent. If you use the program in debug mode
476             ($processes == 0), this method just calls the callback.
477              
478             If the $data_structure_reference is provided, then it is serialized and
479             passed to the parent process. See RETRIEVING DATASTRUCTURES for more info.
480              
481             =item set_max_procs $processes
482              
483             Allows you to set a new maximum number of children to maintain.
484              
485             =item wait_all_children
486              
487             You can call this method to wait for all the processes which have been
488             forked. This is a blocking wait.
489              
490             =item reap_finished_children
491              
492             This is a non-blocking call to reap children and execute callbacks independent
493             of calls to "start" or "wait_all_children". Use this in scenarios where "start"
494             is called infrequently but you would like the callbacks executed quickly.
495              
496             =item is_parent
497              
498             Returns C<true> if within the parent or C<false> if within the child.
499              
500             =item is_child
501              
502             Returns C<true> if within the child or C<false> if within the parent.
503              
504             =item max_procs
505              
506             Returns the maximal number of processes the object will fork.
507              
508             =item running_procs
509              
510             Returns the pids of the forked processes currently monitored by the
511             C<Parallel::ForkManager>. Note that children are still reported as running
512             until the fork manager harvest them, via the next call to
513             C<start> or C<wait_all_children>.
514              
515             my @pids = $pm->running_procs;
516              
517             my $nbr_children = $pm->running_procs;
518              
519             =item running_procs_with_identifiers
520              
521             Returns a list of pairs. The keys are the pids of forked processes (as
522             returned by C<running_procs> and the values are the identifiers given to those
523             pids by C<start>. If no identifier was given for a process, the value for that
524             pid will be undefined.
525              
526             =item wait_for_available_procs( $n )
527              
528             Wait until C<$n> available process slots are available.
529             If C<$n> is not given, defaults to I<1>.
530              
531             =item waitpid_blocking_sleep
532              
533             Returns the sleep period, in seconds, of the pseudo-blocking calls. The sleep
534             period can be a fraction of second.
535              
536             Returns C<0> if disabled.
537              
538             Defaults to 1 second.
539              
540             See I<BLOCKING CALLS> for more details.
541              
542             =item set_waitpid_blocking_sleep $seconds
543              
544             Sets the the sleep period, in seconds, of the pseudo-blocking calls.
545             Set to C<0> to disable.
546              
547             See I<BLOCKING CALLS> for more details.
548              
549             =back
550              
551             =head1 CALLBACKS
552              
553             You can define callbacks in the code, which are called on events like starting
554             a process or upon finish. Declare these before the first call to start().
555              
556             The callbacks can be defined with the following methods:
557              
558             =over 4
559              
560             =item run_on_finish $code [, $pid ]
561              
562             You can define a subroutine which is called when a child is terminated. It is
563             called in the parent process.
564              
565             The parameters of the $code are the following:
566              
567             - pid of the process, which is terminated
568             - exit code of the program
569             - identification of the process (if provided in the "start" method)
570             - exit signal (0-127: signal name)
571             - core dump (1 if there was core dump at exit)
572             - datastructure reference or undef (see RETRIEVING DATASTRUCTURES)
573              
574             =item run_on_start $code
575              
576             You can define a subroutine which is called when a child is started. It called
577             after the successful startup of a child in the parent process.
578              
579             The parameters of the $code are the following:
580              
581             - pid of the process which has been started
582             - identification of the process (if provided in the "start" method)
583              
584             =item run_on_wait $code, [$period]
585              
586             You can define a subroutine which is called when the child process needs to wait
587             for the startup. If $period is not defined, then one call is done per
588             child. If $period is defined, then $code is called periodically and the
589             module waits for $period seconds between the two calls. Note, $period can be
590             fractional number also. The exact "$period seconds" is not guaranteed,
591             signals can shorten and the process scheduler can make it longer (on busy
592             systems).
593              
594             The $code called in the "start" and the "wait_all_children" method also.
595              
596             No parameters are passed to the $code on the call.
597              
598             =back
599              
600             =head1 BLOCKING CALLS
601              
602             When it comes to waiting for child processes to terminate, C<Parallel::ForkManager> is between
603             a fork and a hard place (if you excuse the terrible pun). The underlying Perl C<waitpid> function
604             that the module relies on can block until either one specific or any child process
605             terminate, but not for a process part of a given group.
606              
607             This means that the module can do one of two things when it waits for
608             one of its child processes to terminate:
609              
610             =over
611              
612             =item Only wait for its own child processes
613              
614             This is done via a loop using a C<waitpid> non-blocking call and a sleep statement.
615             The code does something along the lines of
616              
617             while(1) {
618             if ( any of the P::FM child process terminated ) {
619             return its pid
620             }
621              
622             sleep $sleep_period
623             }
624              
625             This is the default behavior that the module will use.
626             This is not the most efficient way to wait for child processes, but it's
627             the safest way to ensure that C<Parallel::ForkManager> won't interfere with
628             any other part of the codebase.
629              
630             The sleep period is set via the method C<set_waitpid_blocking_sleep>.
631              
632             =item Block until any process terminate
633              
634             Alternatively, C<Parallel::ForkManager> can call C<waitpid> such that it will
635             block until any child process terminate. If the child process was not one of
636             the monitored subprocesses, the wait will resume. This is more efficient, but mean
637             that C<P::FM> can captures (and discards) the termination notification that a different
638             part of the code might be waiting for.
639              
640             If this is a race condition
641             that doesn't apply to your codebase, you can set the
642             I<waitpid_blocking_sleep> period to C<0>, which will enable C<waitpid> call blocking.
643              
644             my $pm = Parallel::ForkManager->new( 4 );
645              
646             $pm->set_waitpid_blocking_sleep(0); # true blocking calls enabled
647              
648             for ( 1..100 ) {
649             $pm->start and next;
650              
651             ...; # do work
652              
653             $pm->finish;
654             }
655              
656             =back
657              
658             =head1 RETRIEVING DATASTRUCTURES from child processes
659              
660             The ability for the parent to retrieve data structures is new as of version
661             0.7.6.
662              
663             Each child process may optionally send 1 data structure back to the parent.
664             By data structure, we mean a reference to a string, hash or array. The
665             contents of the data structure are written out to temporary files on disc
666             using the L<Storable> modules' store() method. The reference is then
667             retrieved from within the code you send to the run_on_finish callback.
668              
669             The data structure can be any scalar perl data structure which makes sense:
670             string, numeric value or a reference to an array, hash or object.
671              
672             There are 2 steps involved in retrieving data structures:
673              
674             1) A reference to the data structure the child wishes to send back to the
675             parent is provided as the second argument to the finish() call. It is up
676             to the child to decide whether or not to send anything back to the parent.
677              
678             2) The data structure reference is retrieved using the callback provided in
679             the run_on_finish() method.
680              
681             Keep in mind that data structure retrieval is not the same as returning a
682             data structure from a method call. That is not what actually occurs. The
683             data structure referenced in a given child process is serialized and
684             written out to a file by L<Storable>. The file is subsequently read back
685             into memory and a new data structure belonging to the parent process is
686             created. Please consider the performance penalty it can imply, so try to
687             keep the returned structure small.
688              
689             =head1 EXAMPLES
690              
691             =head2 Parallel get
692              
693             This small example can be used to get URLs in parallel.
694              
695             use Parallel::ForkManager;
696             use LWP::Simple;
697              
698             my $pm = Parallel::ForkManager->new(10);
699              
700             LINKS:
701             for my $link (@ARGV) {
702             $pm->start and next LINKS;
703             my ($fn) = $link =~ /^.*\/(.*?)$/;
704             if (!$fn) {
705             warn "Cannot determine filename from $fn\n";
706             } else {
707             $0 .= " " . $fn;
708             print "Getting $fn from $link\n";
709             my $rc = getstore($link, $fn);
710             print "$link downloaded. response code: $rc\n";
711             };
712             $pm->finish;
713             };
714              
715             =head2 Callbacks
716              
717             Example of a program using callbacks to get child exit codes:
718              
719             use strict;
720             use Parallel::ForkManager;
721              
722             my $max_procs = 5;
723             my @names = qw( Fred Jim Lily Steve Jessica Bob Dave Christine Rico Sara );
724             # hash to resolve PID's back to child specific information
725              
726             my $pm = Parallel::ForkManager->new($max_procs);
727              
728             # Setup a callback for when a child finishes up so we can
729             # get it's exit code
730             $pm->run_on_finish( sub {
731             my ($pid, $exit_code, $ident) = @_;
732             print "** $ident just got out of the pool ".
733             "with PID $pid and exit code: $exit_code\n";
734             });
735              
736             $pm->run_on_start( sub {
737             my ($pid, $ident)=@_;
738             print "** $ident started, pid: $pid\n";
739             });
740              
741             $pm->run_on_wait( sub {
742             print "** Have to wait for one children ...\n"
743             },
744             0.5
745             );
746              
747             NAMES:
748             foreach my $child ( 0 .. $#names ) {
749             my $pid = $pm->start($names[$child]) and next NAMES;
750              
751             # This code is the child process
752             print "This is $names[$child], Child number $child\n";
753             sleep ( 2 * $child );
754             print "$names[$child], Child $child is about to get out...\n";
755             sleep 1;
756             $pm->finish($child); # pass an exit code to finish
757             }
758              
759             print "Waiting for Children...\n";
760             $pm->wait_all_children;
761             print "Everybody is out of the pool!\n";
762              
763             =head2 Data structure retrieval
764              
765             In this simple example, each child sends back a string reference.
766              
767             use Parallel::ForkManager 0.7.6;
768             use strict;
769              
770             my $pm = Parallel::ForkManager->new(2, '/server/path/to/temp/dir/');
771              
772             # data structure retrieval and handling
773             $pm -> run_on_finish ( # called BEFORE the first call to start()
774             sub {
775             my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
776              
777             # retrieve data structure from child
778             if (defined($data_structure_reference)) { # children are not forced to send anything
779             my $string = ${$data_structure_reference}; # child passed a string reference
780             print "$string\n";
781             }
782             else { # problems occurring during storage or retrieval will throw a warning
783             print qq|No message received from child process $pid!\n|;
784             }
785             }
786             );
787              
788             # prep random statement components
789             my @foods = ('chocolate', 'ice cream', 'peanut butter', 'pickles', 'pizza', 'bacon', 'pancakes', 'spaghetti', 'cookies');
790             my @preferences = ('loves', q|can't stand|, 'always wants more', 'will walk 100 miles for', 'only eats', 'would starve rather than eat');
791              
792             # run the parallel processes
793             PERSONS:
794             foreach my $person (qw(Fred Wilma Ernie Bert Lucy Ethel Curly Moe Larry)) {
795             $pm->start() and next PERSONS;
796              
797             # generate a random statement about food preferences
798             my $statement = $person . ' ' . $preferences[int(rand @preferences)] . ' ' . $foods[int(rand @foods)];
799              
800             # send it back to the parent process
801             $pm->finish(0, \$statement); # note that it's a scalar REFERENCE, not the scalar itself
802             }
803             $pm->wait_all_children;
804              
805             A second datastructure retrieval example demonstrates how children decide
806             whether or not to send anything back, what to send and how the parent should
807             process whatever is retrieved.
808              
809             =for example begin
810              
811             use Parallel::ForkManager 0.7.6;
812             use Data::Dumper; # to display the data structures retrieved.
813             use strict;
814              
815             my $pm = Parallel::ForkManager->new(20); # using the system temp dir $L<File::Temp::tempdir()
816              
817             # data structure retrieval and handling
818             my %retrieved_responses = (); # for collecting responses
819             $pm -> run_on_finish (
820             sub {
821             my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
822              
823             # see what the child sent us, if anything
824             if (defined($data_structure_reference)) { # test rather than assume child sent anything
825             my $reftype = ref($data_structure_reference);
826             print qq|ident "$ident" returned a "$reftype" reference.\n\n|;
827             if (1) { # simple on/off switch to display the contents
828             print &Dumper($data_structure_reference) . qq|end of "$ident" sent structure\n\n|;
829             }
830              
831             # we can also collect retrieved data structures for processing after all children have exited
832             $retrieved_responses{$ident} = $data_structure_reference;
833             } else {
834             print qq|ident "$ident" did not send anything.\n\n|;
835             }
836             }
837             );
838              
839             # generate a list of instructions
840             my @instructions = ( # a unique identifier and what the child process should send
841             {'name' => '%ENV keys as a string', 'send' => 'keys'},
842             {'name' => 'Send Nothing'}, # not instructing the child to send anything back to the parent
843             {'name' => 'Childs %ENV', 'send' => 'all'},
844             {'name' => 'Child chooses randomly', 'send' => 'random'},
845             {'name' => 'Invalid send instructions', 'send' => 'Na Na Nana Na'},
846             {'name' => 'ENV values in an array', 'send' => 'values'},
847             );
848              
849             INSTRUCTS:
850             foreach my $instruction (@instructions) {
851             $pm->start($instruction->{'name'}) and next INSTRUCTS; # this time we are using an explicit, unique child process identifier
852              
853             # last step in child processing
854             $pm->finish(0) unless $instruction->{'send'}; # no data structure is sent unless this child is told what to send.
855              
856             if ($instruction->{'send'} eq 'keys') {
857             $pm->finish(0, \join(', ', keys %ENV));
858              
859             } elsif ($instruction->{'send'} eq 'values') {
860             $pm->finish(0, [values %ENV]); # kinda useless without knowing which keys they belong to...
861              
862             } elsif ($instruction->{'send'} eq 'all') {
863             $pm->finish(0, \%ENV); # remember, we are not "returning" anything, just copying the hash to disc
864              
865             # demonstrate clearly that the child determines what type of reference to send
866             } elsif ($instruction->{'send'} eq 'random') {
867             my $string = q|I'm just a string.|;
868             my @array = qw(I am an array);
869             my %hash = (type => 'associative array', synonym => 'hash', cool => 'very :)');
870             my $return_choice = ('string', 'array', 'hash')[int(rand 3)]; # randomly choose return data type
871             $pm->finish(0, \$string) if ($return_choice eq 'string');
872             $pm->finish(0, \@array) if ($return_choice eq 'array');
873             $pm->finish(0, \%hash) if ($return_choice eq 'hash');
874              
875             # as a responsible child, inform parent that their instruction was invalid
876             } else {
877             $pm->finish(0, \qq|Invalid instructions: "$instruction->{'send'}".|); # ordinarily I wouldn't include invalid input in a response...
878             }
879             }
880             $pm->wait_all_children; # blocks until all forked processes have exited
881              
882             # post fork processing of returned data structures
883             for (sort keys %retrieved_responses) {
884             print qq|Post processing "$_"...\n|;
885             }
886              
887             =for example end
888              
889             =head1 USING RAND() IN FORKED PROCESSES
890              
891             A caveat worth noting is that all forked processes will use the
892             same random seed, so potentially providing the same results (see
893             L<http://blogs.perl.org/users/brian_phillips/2010/06/when-rand-isnt-random.html>).
894             If you are using C<rand()> and want each forked child to use a different seed, you
895             can add the following to your program:
896              
897             $pm->run_on_start(sub { srand });
898              
899             =head1 EXTENDING
900              
901             As of version 2.0.0, C<Parallel::ForkManager> uses L<Moo> under the hood. When
902             a process is being forked from the parent object, the forked instance of the
903             object will be modified to consume the L<Parallel::ForkManager::Child>
904             role. All of this makes extending L<Parallel::ForkManager> to implement
905             any storing/retrieving mechanism or any other behavior fairly easy.
906              
907             =head2 Example: store and retrieve data via a web service
908              
909             {
910             package Parallel::ForkManager::Web;
911              
912             use HTTP::Tiny;
913              
914             use Moo;
915             extends 'Parallel::ForkManager';
916              
917             has ua => (
918             is => 'ro',
919             lazy => 1,
920             default => sub {
921             HTTP::Tiny->new;
922             }
923             );
924              
925             sub store {
926             my( $self, $data ) = @_;
927              
928             $self->ua->post( "http://.../store/$$", { body => $data } );
929             }
930              
931             sub retrieve {
932             my( $self, $kid_id ) = @_;
933              
934             $self->ua->get( "http://.../store/$kid_id" )->{content};
935             }
936              
937             }
938              
939             my $fm = Parallel::ForkManager::Web->new(2);
940              
941             $fm->run_on_finish(sub{
942             my $retrieved = $_[5];
943              
944             print "got ", $retrieved, "\n";
945             });
946              
947             $fm->start_child(sub {
948             return $_**2;
949             }) for 1..3;
950              
951             $fm->wait_all_children;
952              
953             =head2 Example: have the child processes exit differently
954              
955             use Parallel::ForkManager;
956              
957             package Parallel::ForkManager::Child::PosixExit {
958             use Moo::Role;
959             with 'Parallel::ForkManager::Child';
960              
961             sub finish { POSIX::_exit() };
962             }
963              
964             my $fm = Parallel::ForkManager->new(
965             max_proc => 1,
966             child_role => 'Parallel::ForkManager::Child::PosixExit'
967             );
968              
969             =head1 SECURITY
970              
971             Parallel::ForkManager uses temporary files when
972             a child process returns information to its parent process. The filenames are
973             based on the process of the parent and child processes, so they are
974             fairly easy to guess. So if security is a concern in your environment, make sure
975             the directory used by Parallel::ForkManager is restricted to the current user
976             only (the default behavior is to create a directory,
977             via L<File::Temp>'s C<tempdir>, which does that).
978              
979             =head1 TROUBLESHOOTING
980              
981             =head2 PerlIO::gzip and Parallel::ForkManager do not play nice together
982              
983             If you are using L<PerlIO::gzip> in your child processes, you may end up with
984             garbled files. This is not really P::FM's fault, but rather a problem between
985             L<PerlIO::gzip> and C<fork()> (see L<https://rt.cpan.org/Public/Bug/Display.html?id=114557>).
986              
987             Fortunately, it seems there is an easy way to fix the problem by
988             adding the "unix" layer? I.e.,
989              
990             open(IN, '<:unix:gzip', ...
991              
992             =head1 BUGS AND LIMITATIONS
993              
994             Do not use Parallel::ForkManager in an environment where other child
995             processes can affect the run of the main program; using this module
996             is not recommended in an environment where fork() / wait() is already used.
997              
998             If you want to use more than one copies of the Parallel::ForkManager, then
999             you have to make sure that all children processes are terminated, before you
1000             use the second object in the main program.
1001              
1002             You are free to use a new copy of Parallel::ForkManager in the child
1003             processes, although I don't think it makes sense.
1004              
1005             =head1 CREDITS
1006              
1007             Michael Gang (bug report)
1008             Noah Robin <sitz@onastick.net> (documentation tweaks)
1009             Chuck Hirstius <chirstius@megapathdsl.net> (callback exit status, example)
1010             Grant Hopwood <hopwoodg@valero.com> (win32 port)
1011             Mark Southern <mark_southern@merck.com> (bugfix)
1012             Ken Clarke <www.perlprogrammer.net> (datastructure retrieval)
1013              
1014             =head1 AUTHORS
1015              
1016             =over 4
1017              
1018             =item *
1019              
1020             dLux (Szabó, Balázs) <dlux@dlux.hu>
1021              
1022             =item *
1023              
1024             Yanick Champoux <yanick@cpan.org>
1025              
1026             =item *
1027              
1028             Gabor Szabo <gabor@szabgab.com>
1029              
1030             =back
1031              
1032             =head1 COPYRIGHT AND LICENSE
1033              
1034             This software is copyright (c) 2025, 2015 by Balázs Szabó.
1035              
1036             This is free software; you can redistribute it and/or modify it under
1037             the same terms as the Perl 5 programming language system itself.
1038              
1039             =cut