Parallel :: ForkManager分叉后只有一个活动线程。为什么?

时间:2014-08-10 13:27:11

标签: multithreading perl fork

我在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个线程都在运行。为什么只有一个?

1 个答案:

答案 0 :(得分:0)

尝试在for循环之前为任何活动数据库或语句句柄设置AutoInactiveDestroy