在使用Parallel::Fork进行分叉后,我只得到了一个工作线程:
ps -ef | grep ./BuildReportIndexV_VR2.pl
503 15955 9531 18 13:11 pts/0 00:02:06 /usr/bin/perl ./BuildReportIndexV_VR2.pl promt_tenant=AD backward=1
503 16102 15955 99 13:13 pts/0 00:09:29 /usr/bin/perl ./BuildReportIndexV_VR2.pl promt_tenant=AD backward=1
503 16103 15955 0 13:13 pts/0 00:00:02 /usr/bin/perl ./BuildReportIndexV_VR2.pl promt_tenant=AD backward=1
503 16104 15955 0 13:13 pts/0 00:00:02 /usr/bin/perl ./BuildReportIndexV_VR2.pl promt_tenant=AD backward=1
503 16105 15955 0 13:13 pts/0 00:00:03 /usr/bin/perl ./BuildReportIndexV_VR2.pl promt_tenant=AD backward=1
15955进程是父进程,16102及以下是子进程。
以下是相关代码:
my $pm = Parallel::ForkManager->new($MAX_PROCESSES);
$pm->set_max_procs( $MAX_PROCESSES );
my $start_index;
my $last_index;
for ( $start_index = 0,
$last_index=$start_index+$subhash_size-1;
$last_index <= keys %$index_hr;
$start_index=$last_index+1, $last_index=$start_index+$subhash_size-1
)
{
$pm->start and next;
create_report_subprocess( $index_hr, $start_index, $last_index );
$pm->finish;
}
$pm->wait_all_children;
print "After all children are waited: last index: $last_index\n";
if ( $last_index > keys %$index_hr) {
$last_index = keys %$index_hr;
create_report_subprocess( $index_hr, $start_index, $last_index );
}
我将$MAX_PROCESSES设置为4。
我期望所有4个线程都在运行。为什么只有一个?
($x += $_) -= $_ for 1..100000000;
,我得到所有进程报告99%的cpu(有4个可用核心),在一个单核vm中,它们各自报告24%的cpu。如果你尝试一下,你会看到什么? - ysth