I have a script that uses Parallel::ForkManager. However, the wait_all_children() process takes incredibly long time even after all child-processes are completed. The way I know is by printing out some timestamps (see below). Does anyone have any idea what might be causing this (I have 16 CPU cores on my machine)?
my $pm = Parallel::ForkManager->new(16);
for my $i (1..16) {
$pm->start($i) and next;
... do something within the child-process ...
print (scalar localtime), " Process $i completed.\n";
$pm->finish();
}
print (scalar localtime), " Waiting for some child process to finish.\n";
$pm->wait_all_children();
print (scalar localtime), " All processes finished.\n";
Clearly, I’ll get the Waiting for some child process to finish message first, with a timestamp of, say, 7:08:35. Then I’ll get a list of Process i completed messages, with the last one at 7:10:30. However, I do not receive the message All Processes finished until 7:16:33(!). Why is that 6-minute delay between 7:10:30 and 7:16:33? Thx!
I tried this:
I got:
I have
perl 5.10.1on Linux with Parallel::ForkManager version0.7.5.Therefore, I conclude that whatever issue you are having is happening as a consequence of what happens when you
Update: The problem is, you are printing the
Process finished messagebefore the thefinishcall. Try the following version:See Callbacks in Parallel::ForkManager documentation for more information. If the delay disappears, then the symptom you are observing was due to the fact that you were claiming the forked process had finished before it was done.