我想使用Parallel::ForkManager
获取所有孩子的所有元素的哈希值。
use strict;
use Parallel::ForkManager;
my @arr2 = (a, b, c, d);
foreach ( @arr2 ) {
$pid = $pm->start
$hash{$_} = localtime;
# STORE ABOVE ELEMENT TO EXIST HASH
my $pid = $pm->start and next;
}
$pm->wait_all_children;
READ %hash
$hash{a} = 11:02
$hash{b} = 11:03
$hash{c} = 11:04
$hash{d} = 11:05
是否可以通过run_on_exit
回调共享哈希元素?或者我必须使用外部文件?哪个模块以最简单的方式完成?我尝试了IPC :: Shareable和DBM :: Deep。它们不适用于我的脚本。
答案 0 :(得分:-1)
线程可以轻松共享数据:
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use Thread::Queue;
my $q = 'Thread::Queue'->new;
my @workers = map 'threads'->create(sub {
$q->enqueue([ 'threads'->tid, scalar localtime ]);
}), 1 .. 4;
$_->join for @workers;
my %hash;
while ($q->pending) {
my $data = $q->dequeue;
$hash{ $data->[0] } = $data->[1];
}
use Data::Dumper; print Dumper \%hash;