Multithreading Perl-在处理多线程数据时将散列出列
我有一个非常大的散列,有超过50000个条目。由于时间限制,我想处理这个多线程 是否每次调用dequeue()都可能返回哈希中的下一项,而不是完整的哈希?在下面的示例中,我希望dequeue()只返回:Multithreading Perl-在处理多线程数据时将散列出列,multithreading,perl,hash,queue,Multithreading,Perl,Hash,Queue,我有一个非常大的散列,有超过50000个条目。由于时间限制,我想处理这个多线程 是否每次调用dequeue()都可能返回哈希中的下一项,而不是完整的哈希?在下面的示例中,我希望dequeue()只返回: flintstones => { old_address => "0x1231234a", new_address => "0x1234234d", source => "sym" }, 然后,我可以在我的线程中处理它,同时另一个线程从
flintstones => {
old_address => "0x1231234a",
new_address => "0x1234234d",
source => "sym"
},
然后,我可以在我的线程中处理它,同时另一个线程从散列中取出另一个项目,直到处理完所有项目。下面是我的代码示例
如果我需要更改存储格式(HoH),这不是问题。也许一组散列可以工作?感谢您的帮助/指点
use strict;
use warnings;
use threads;
use Thread::Queue;
use Data::Dumper;
my %hoh = (
flintstones => {
old_address => "0x1231234a",
new_address => "0x1234234d",
source => "sym"
},
jetsons => {
old_address => "0x12712343",
new_address => "0x12142344",
source => "sym"
},
simpsons => {
old_address => "0x12f12347",
new_address => "0x12a42348",
source => "dwarf"
},
);
my $href = \%hoh;
my $queue= Thread::Queue->new($href);
my $t = threads->create('start_sub');
my $result = $t->join;
sub start_sub {
print "items on queue = " . $queue->pending() . "\n";
while( $queue->pending() ) {
my $item = $queue->dequeue_nb();
#
## dequeue_nb returns undef when queue empty
if( $item ) {
print "Doing work in thread " . threads->tid() . " on:\n";
print Dumper($item);
print "Done =====================\n"
}
}
}
但如果使用引用而不是哈希,则效果会更好。使用包含hashref的arrefrefref。这样会更有效率 这适用于您当前的代码
sub dequeue_nb{
my $key_of_first_item = (keys %hoh)[0];#random order
my $item = $hoh{$key_of_first_item};
delete $hoh{$key_of_first_item};
return $item;
关于,dequeue\u nb来自模块Thread::Queue。我不确定重新定义该函数是我想在这里做的事情……我想我已经用
my$queue=Thread::queue->new(键%hoh)解决了这个问题代码>。基本上,将散列的键加载到队列中,然后每个线程可以处理单个项。。。