Perl的并行:: Forkmanager不允许收集变量值 [英] Perl Parallel::Forkmanager doesn't allow to collect variable values

查看:889
本文介绍了Perl的并行:: Forkmanager不允许收集变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是因为子进程不知道我的哈希值(见下面的code)中,哈希%输出没有任何收集..有任何其他方式,从写一个tmp文件分开收集价值?

Maybe because the child processes are not aware of my hash (see the below code), the hash %output is not collecting anything .. is there any other way to collect the value apart from writing a tmp file ?

foreach $Item (@AllItems) {
$pid = $pm->start($Item) and next;
$Temp = qx($Item);
$output{$Item}= $Temp; // This doesn't collect anything. :-(
$pm->finish;
}

$pm->wait_all_children;

TIA,
蒂姆·

TIA, Tim

推荐答案

派生进程都有自己的拷贝(当然,副本上写)父进程的内存。写在子进程中的散列不会影响父母的哈希值。

Forked processes have their own copies (well, copies-on-write) of the parent process's memory. Writing to the hash in a child process won't affect the hash in the parent.

要做到你想要什么,你都需要采用某种形式的IPC。请参阅 perlipc 联机帮助页的各种可能性了长时间的讨论。

To do what you want, you'll need to employ some sort of IPC. See the perlipc manpage for a lengthy discussion of the various possibilities.

有关这样的事情,我可能会使用一些简单的像磁盘上的哈希值。 DB_File 提供一个很好的捆绑的散列接口。这里是你将如何做到这一点:

For something like this, I'd probably use something simple like an on-disk hash. DB_File provides a nice tied hash interface. Here's how you might do it:

use strict;
use warnings;

use DB_File;

tie my %output, "DB_File", "output.dat" ;

foreach my $item( @AllItems) { 
    my $pid = $pm->start and next;
    $output{$item} = qx($item);
    $pm->finish;
}

这篇关于Perl的并行:: Forkmanager不允许收集变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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