windows - Why doesn't this fork loop work as expected? -
why following code:
#!/usr/bin/perl use strict; use warnings; use parallel::forkmanager; $pm = new parallel::forkmanager(5); @all_data = ('a','b','c','d','e','f'); foreach $data (@all_data) { # forks , returns pid child: $pid = $pm->start , next; print "hello $pid\n"; $pm->finish; # terminates child process } $pm->wait_all_children;
print:
hello 0 hello 0 hello 0 hello 0 hello 0
i new perl , trying catch on multiprocessing in perl
from docs start
method:
this method fork. returns pid of child process parent, , 0 child process.
as happens, fork
function same, why start
mirrors it.
the parent may need child pid control child – sending signals , stuff – child knows own pid via $$
variable:
foreach $data (@all_data) { $pm->start , next; print "hello $$\n"; $pm->finish; }
example output:
hello 22215 hello 22218 hello 22219 hello 22217 hello 22220 hello 22216
Comments
Post a Comment