我正在尝试使用 Storable 来帮助收集由多个线程生成的数据。没有线程部分,可存储的代码位运行良好。但是,使用我提供的示例代码,我收到以下错误:
thread failed to start: Invalid value for shared scalar at ./storable2.pl line 55.
第 55 行是$hash{"Simpsons"}{"Krusty"} = \%test2_hash;
@ikegami 在较早的帖子中告诉我,错误是由于我的引用指向一个非共享变量而引起的。我尝试与代码共享变量my %test2_hash : shared;
(这是示例代码的一部分),但我想我使用不正确。
我还尝试创建一个共享的新哈希:
my %shared_hash;
%test2_hash = %{retrieve($tmp_filename)};
%shared_hash = share(%test2_hash);
但是,这会导致相同的错误。
这是我的示例代码:
#!/usr/bin/perl
# storable.pl
use strict;
use warnings;
use Storable qw(store retrieve);
use File::Temp;
use threads;
use threads::shared;
use Thread::Queue;
use constant NUM_WORKERS => 10;
use Data::Dumper qw(Dumper);
my @out_array;
main();
sub main
{
start_threads();
foreach my $item (@out_array) {
print "item: $item\n";
}
}
sub start_threads
{
my $queue = Thread::Queue->new();
foreach (1..NUM_WORKERS) {
async {
while (my $job = $queue->dequeue()) {
test1($job);
}
};
}
my @sentiments = ("Axe Murderer", "Mauler", "Babyface", "Dragon");
$queue->enqueue(@sentiments);
$queue->enqueue(undef) for 1..NUM_WORKERS;
$_->join() for threads->list();
my @return_array = @out_array;
return @return_array;
}
sub test1
{
my $fighter = $_[0];
my $tmp_filename : shared;
my %hash : shared;
my %test2_hash : shared;
$tmp_filename = get_temp_filename();
test2($fighter, $tmp_filename);
%test2_hash = %{retrieve($tmp_filename)};
$hash{"Simpsons"}{"Krusty"} = \%test2_hash;
push @out_array, \%test2_hash;
return;
}
sub test2
{
my ($fighter, $tmp_filename) = @_;
my %test2_hash;
$test2_hash{"Zuffa"}{"LLC"} = $fighter;
store(\%test2_hash, $tmp_filename);
}
sub get_temp_filename {
my $fh = File::Temp->new(
TEMPLATE => 'tempXXXXX',
DIR => 'tmpdir',
SUFFIX => '.tmp',
);
return $fh->filename;
}
与 Thread::Queue 一起实现时使用 Storable 的正确方法是什么?还是问题纯粹与不正确的 Thread::Queue 实现有关?
抱歉@ikegami :( 我真的尝试过使用我拥有的工具,但我又碰壁了。
更新:我更新了代码。使用严格和警告我得到以下输出:
thread failed to start: Invalid value for shared scalar at ./storable2.pl line 52.
第 52 行是%test2_hash = %{retrieve($tmp_filename)};