我有一个使用Parallel::ForkManager
的脚本。但是,即使所有子进程都已完成,wait_all_children()进程也要花费非常长的时间。我知道的方式是打印一些时间戳(请参见下文)。有谁知道这可能是什么原因(我的计算机上有16个CPU内核)?
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";
显然,我将首先获得
Waiting for some child process to finish
消息,并带有一个时间戳,例如7:08:35
。然后,我将获得Process i completed
消息列表,最后一个消息位于7:10:30
。但是,直到All Processes finished
(!),我才收到消息7:16:33
。为什么在7:10:30和7:16:33之间有6分钟的延迟?谢谢! 最佳答案
我尝试了这个:
#!/opt/perl/bin/perl
use strict; use warnings;
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(16);
for my $i (1..16) {
$pm->start($i) and next;
sleep rand 20;
printf "%s : Process %d completed\n", scalar localtime, $i;
$pm->finish;
}
printf "%s: Waiting for some child to finish\n", scalar localtime;
$pm->wait_all_children;
printf "%s: All processes finished.\n", scalar localtime;
我有:
[sinan@archardy Src]$ ./y.pl Thu Mar 11 17:14:16 2010 : Process 3 completed Thu Mar 11 17:14:16 2010: Waiting for some child to finish Thu Mar 11 17:14:18 2010 : Process 8 completed Thu Mar 11 17:14:18 2010 : Process 14 completed <snip>...</snip> Thu Mar 11 17:14:34 2010 : Process 12 completed Thu Mar 11 17:14:34 2010: All processes finished.
I have perl 5.10.1
on Linux with Parallel::ForkManager version 0.7.5
.
Therefore, I conclude that whatever issue you are having is happening as a consequence of what happens when you
# ... do something within the child-process ...
更新:问题是,您正在
Process finished message
调用之前打印finish
。尝试以下版本:#!/opt/perl/bin/perl
use strict; use warnings;
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(16);
$pm->run_on_finish( sub {
printf "%s : Process completed: @_\n", scalar localtime
});
for my $i (1..16) {
$pm->start($i) and next;
sleep rand 20;
$pm->finish;
}
printf "%s: Waiting for some child to finish\n", scalar localtime;
$pm->wait_all_children;
printf "%s: All processes finished.\n", scalar localtime;
有关更多信息,请参见Callbacks in Parallel::ForkManager documentation。如果延迟消失,那么您所观察到的症状是由于您声称分叉的过程在完成之前已经完成。