user3003367
user3003367

Reputation: 31

Perl Multithread Program

I am new in Perl. I want to write a Perl script using thread.I have few files say 20 files and want to process those files using 5 threads in 4 batches. I am printing the thread no. After completing one batch ,the thread no must start with 1 for the next batch. But instead of that its creating 20 threads.please help. my code is as follows:

#!/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";
    }

Edited program using thread queue:

    #!/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;
    }

Upvotes: 0

Views: 1290

Answers (2)

user1126070
user1126070

Reputation: 5069

You could use Paralel:Queue and create 4 thread and pass them items that they could work on.

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

Upvotes: 1

pilcrow
pilcrow

Reputation: 58711

You are spawning a thread and then waiting for it to complete before spawning the next, each thread handling one file. That is why you see as many threads as you have files.

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

Instead try something like this:

....

# 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;
}

As an aside, Thread::Queue and Thread-Pool might imply better designs for the work you want to do.

Upvotes: 2

Related Questions