Perl Parallel :: ForkManager空返回 [英] Perl Parallel::ForkManager empty return

查看:137
本文介绍了Perl Parallel :: ForkManager空返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Parallel :: ForkManager并行运行procedure,但是不幸的是,子例程并行不返回任何条目.

I am trying to use Parallel::ForkManager to run proceed parallel but unfortunately the subroutine parallel does not return any entries.

 sub parallel {
   my ($self,$values) = @_;
   my %hash;
   my $pm = Parallel::ForkManager->new(200);
   foreach my $IP ( keys %{$values} ) { 
    my $pid = $pm->start and next;
    $hash{$IP}=$self->getData($IP);
    $pm->finish(0, \$hash{$IP});
   }
   $pm->wait_all_children;
   return %hash;
  }



print Dumper( parallel(%data) );

我做错了什么?有什么想法吗?

What I'm doing wrong? Any ideas?

推荐答案

分叉是创建新进程的过程,它是当前进程的副本.在一个进程中更改变量不会更改在其他进程中名称相似的变量.

Forking is the creation of a new process that's a copy of the current process. Changing a variable in one process doesn't change similarly named variables in other processes.

您修改了子进程的%hash,但您正在转储父进程的%hash.

You modify the child's process's %hash, but you're dumping the parent's process's %hash.

P :: FM确实提供了一种将数据传递回父进程的机制. 已记录,标题为从子进程中检索数据结构".

P::FM does provide a mechanism for passing data back to the parent process. It's documented under the heading "RETRIEVING DATASTRUCTURES from child processes".

use Data::Dumper          qw( Dumper );
use Parallel::ForkManager qw( );

use constant MAX_WORKERS => 200;

my %hash;

my $pm = Parallel::ForkManager->new(MAX_WORKERS);
$pm->run_on_finish(sub {
   my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $result_ref) = @_;

   my $IP = $ident;

   warn("Child $IP killed by signal $exit_signal"), return if $exit_signal;
   warn("Child $IP exited with error $exit_code"),  return if $exit_code;
   warn("Child $IP encountered an unknown error"),  return if !$result_ref;

   $hash{$IP} = $$result_ref;
});

for my $IP (keys %$values) { 
   my $pid = $pm->start($IP) and next;
   $pm->finish(0, \$self->getData($IP));
}

$pm->wait_all_children();

print(Dumper(\%hash));

这篇关于Perl Parallel :: ForkManager空返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆