Perl - 处理多线程时出列哈希值

时间:2012-03-08 10:47:32

标签: multithreading perl hash queue

我有一个非常大的hash-of-hash,有超过50,000个条目。由于时间限制,我想处理这个多线程。

每次调用dequeue()是否有可能从散列中返回下一个项目,而不是完整的散列?在下面的例子中,我想让dequeue()返回:

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"
         }
    }  
}

1 个答案:

答案 0 :(得分:0)

但是如果你使用引用而不是哈希会更好。使用包含hashrefs的arrefref。那会更有效率。

这是您当前的代码。

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;

的问候,