Title: Threads in Perl

Dear Gurus,

I need to use threads in Perl. I have following requirement. I need to process few files at a given time, I used to use following approach

1) Get the names of all the files in Array
2) Process all the files one by one.

This was a simple one, but it takes lot of time and hence I decided to use threads. What I wanted was
1) Get the names of all the files in Array
2) Process 2 files at a given time.
3) In case any of the file finishes processing, immediately process next file from the array. So that at any given instance, script should process 2 files (Of course excluding the last instance).

My code is as follows

=============
use strict;
use warnings;
use threads;

my @dots ;
my ($dir_path1) = @ARGV;

opendir(DIR,$dir_path1)or die "cannot open : $!";
        @dots = grep {/.*\.(?:rtf)/is} readdir(DIR);
closedir DIR;

my $count_files = @dots;
my $file_counter;
$file_counter = 0;

sub run_macro($)
{
  my ($file) = @_;
  my $path = "$dir_path1\\$file";
  ######## This is the command which is processing the file. ####################
  system ("perl macro.pl $path");
 #####################################################################
}
while($file_counter < $count_files)
{
        my $t1 = threads->create("run_macro",$dots[$file_counter]);
        $file_counter++;
        my $t2 = threads->create("run_macro",$dots[$file_counter]);
        $file_counter++;
        $t1->join();
        $t2->join();
        }
               
===============

This script is processing a batch of 2 files at a time, which I don't want. If some file takes more time to process, then the other thread doesn't catch the next available file in array, but waits till the first thread finishes. The script is processing files in the batch of 2 files. Could you please provide any suggestions?

I request any advice on this. I am using threading first time and hence I apologize in case this question seems too basic.


Thanks & Regards,
Pravin Talegaonkar

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to