Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 如何重用Thread::queue中的队列?_Multithreading_Perl_Queue - Fatal编程技术网

Multithreading 如何重用Thread::queue中的队列?

Multithreading 如何重用Thread::queue中的队列?,multithreading,perl,queue,Multithreading,Perl,Queue,我曾在这里得到过一些指导,其中有以下片段: my $q = Thread::Queue->new(); sub worker { my ($job, $action) = @_; Build($job, $action); } for (1..NUM_WORKERS) { async { while (defined(my $job = $q->dequeue())) { worker($job, 'clean'); }

我曾在这里得到过一些指导,其中有以下片段:

my $q = Thread::Queue->new();
sub worker {
    my ($job, $action) = @_;
    Build($job, $action);
}

for (1..NUM_WORKERS) {
  async {
     while (defined(my $job = $q->dequeue())) {
        worker($job, 'clean');
     }
  };
}

$q->enqueue($_) for @compsCopy;

# When you're done adding to the queue.
$q->end();
$_->join() for threads->list();
重用
q
的最佳选项是什么?目前,我正在创建新的
q
对象,
q2
q3
,并对我要执行的每个
$action
重新执行所有这些操作。还有更好的办法吗?我可能会传入一系列我想要执行的“操作”,如果可能的话,我希望避免将此代码重复7次


也许我不完全理解什么是
Thread::Queue

您应该使用一个队列作为一个方向。如果您只想并行执行某些操作,请使用一个队列。若您想报告错误并在主线程或其他线程中处理这些错误,那个么您可以使用两个队列

为便于使用,以下内容仅供参考:

use strict;
use warnings;
use threads;

use threads;
use Thread::Queue;

my $q = Thread::Queue->new();    # A new empty queue
my %seen: shared;

# Worker thread
my @thrs = threads->create(\&doOperation ) for 1..5;#for 5 threads
add_file_to_q('/tmp/');
$q->enqueue('//_DONE_//') for @thrs;
$_->join() for @thrs;

sub add_file_to_q {
  my $dir = shift;
  my @files = `ls -1 $dir/`;chomp(@files);
  #add files to queue
  foreach my $f (@files){
    # Send work to the thread
    $q->enqueue($f);
    print "Pending items: "$q->pending()."\n";
  }
}



sub doOperation () {
    my $ithread = threads->tid() ;
    while (my $filename = $q->dequeue()) {
      # Do work on $item
      sleep(1) if ! defined $filename;
      return 1 if $filename eq '//_DONE_//';
      next if $seen{$filename};
      print "[id=$ithread]\t$filename\n";
      $seen{$filename} = 1;
      ### add files if it is a directory (check with symlinks, no file with //_DONE_// name!)
      add_file_to_q($filename) if -d $filename;
    }
    return 1;
}

同样,只需在队列中输入完成作业所需的任何信息。如果这包括一种工作类型,那就这样吧。您已经获得了执行此操作的代码:
->enqueue([$job\u type,@job\u params])
Re“也许我不完全理解线程::队列是什么”,它是一个。将其视为一个仅支持
推送
(排队)和
移位
(出列)的数组。T::Q有一个显著的特性,即如果没有任何东西要出列(除非调用了
end
),客户端会阻止尝试出列。创建工作线程<代码>异步{…}是一种干净的编写
线程->创建(sub{…})的方法。真的吗?我必须解释什么是for循环
$\uu
被别名为
@compsCopy
的一个元素,依次为每个元素。例如,如果
@compsCopy
有5个元素,
$q->enqueue($\uq)
将执行5次。第一次,
$将别名为
$compsCopy[0]
;第二个,到
$compsCopy[1]
;等。
$\uuuu
只是一个普通变量//当它们从
异步
的参数块返回时(当它们从传递到
创建
的子块返回时)。