我有一个使用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分钟的延迟? THX!
答案 0 :(得分:8)
我试过了:
#!/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.
我在Parallel::ForkManager版本perl 5.10.1
的Linux上0.7.5
。
因此,我得出结论,无论您遇到什么问题,都是因为当您
时发生的事情# ... 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。如果延迟消失,那么您观察到的症状是由于您声称分叉过程在完成之前已完成。