Perl多线程程序

时间:2014-05-05 10:35:06

标签: multithreading perl

我是Perl的新手。我想使用thread编写一个Perl脚本。我有几个文件说20个文件,并希望使用5个线程分4批处理这些文件。我正在打印线程号。完成一个批处理后,线程no必须从1开始,用于下一批。但不是它创建20个threads.please帮助。我的代码如下:

#!/usr/bin/perl -w
use strict;
use warnings;
use threads;
use threads::shared;

my $INPUT_DIR="/home/Documents/myscript/IMPORTLDIF/";
opendir(DIR, $INPUT_DIR) ;
my @files = grep { /^InputFile/ } readdir DIR;
my $count = @files;
#print "Total Files: $count \n";
my @threads;
my $noofthread = 5;
my $nooffiles = $count;
my $noofbatch = $nooffiles / $noofthread;
#print "No of batch: $noofbatch \n";

my $fileIndex = 0;
my $batch = 1;
while ($fileIndex < $nooffiles) {
    print "Batch: $batch \n";
    for (my $i=0; $i < $noofthread && $fileIndex < $nooffiles ; $i++) {

        my $t = threads->new(\&doOperation, $files[$fileIndex], $i)->join;
        push(@threads, $t);
        $fileIndex++;
        print "FileIndex: $fileIndex \n";
    }
    $batch++;
}

sub doOperation () {
    my $ithread = threads->tid() ;
    print "Thread Index : [id=$ithread]\n" ;
    foreach my $item (@_){
        my $filename = $item;
        print "Filename name: $filename \n";
    }

使用线程队列编辑程序:

    #!/usr/bin/perl -w 
    # This is compiled with threading support

    use strict;
    use warnings;

    use threads;
    use Thread::Queue;

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

    # Worker thread
    my $INPUT_DIR="/home/Documents/myscript/IMPORTLDIF/";
    opendir(DIR, $INPUT_DIR) or die "Cannot opendir: $!";

    my @thrs = threads->create(\&doOperation ) for 1..5;#for 5 threads
    #my @files = `ls -1 /home/Documents/myscript/IMPORTLDIF/`;
    my @files = grep { /^Input/ } readdir DIR or die "File not present present. \n";
    chomp(@files);

    #add files to queue
    foreach my $f (@files){
    # Send work to the thread
    $q->enqueue($f);
    print "Pending items: " + $q->pending()."\n";
    }

    $q->enqueue('_DONE_') for @thrs;
    $_->join() for @thrs;



    sub doOperation () {
    my $ithread = threads->tid() ;
    while (my $filename = $q->dequeue()) {
     # Do work on $item
    return 1 if $filename eq '_DONE_';
    print "[id=$ithread]\t$filename\n";
   }
    return 1;
    }

2 个答案:

答案 0 :(得分:2)

您正在产生一个线程,然后在产生下一个线程之前等待它完成,每个线程处理一个文件。这就是为什么你会看到与文件一样多的线程。

my $t = threads->new(\&doOperation, $files[$fileIndex], $i)->join;
                                                             ^^^^--- This will block

而是尝试这样的事情:

....

# split the workload into N batches
#
while (my @batch = splice(@files, 0, $batch_size)) {
  push @threads, threads->new(\&doOperation, @batch);
}

# now wait for all workers to finish
#
for my $thr (@threads) {
  $thr->join;
}

顺便说一句,Thread::QueueThread-Pool可能意味着您想要做的工作有更好的设计。

答案 1 :(得分:1)

你可以使用Parallel:Queue并创建4个线程并传递它们可以使用的项目。

To fork or not to fork?

use strict;
use warnings;

use threads;
use Thread::Queue;

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

# Worker thread
my @thrs;
push @thrs, threads->create(\&doOperation ) for 1..5;#for 5 threads
my @files = `ls -1 /tmp/`;chomp(@files);
#add files to queue
foreach my $f (@files){
  # Send work to the thread
  $q->enqueue($f);
  print "Pending items: "$q->pending()."\n";
}
$q->enqueue('_DONE_') for @thrs;
$_->join() for threads->list();



sub doOperation () {
    my $ithread = threads->tid() ;
    while (my $filename = $q->dequeue()) {
      # Do work on $item
      return 1 if $filename eq '_DONE_';
      print "[id=$ithread]\t$filename\n";
    }
    return 1;
}