I working on below script in which I split file content in @file_splitted
and trying to apply Thread::Queue to speed up the process. But the $result
returns nothing at the end. Can you please check what happening?
my $NUM_WORKERS = 5;
my $q = Thread::Queue->new();
sub worker {
my ($job) = @_;
print "@_ \n################\n";
my ($sub_name, @args) = @$job;
my $sub_ref = \&Subroutine;
$sub_ref->(@args);
}
{
my $q = Thread::Queue->new();
my @workers;
for (1..$NUM_WORKERS) {
push @workers, async {
while (my $job = $q->dequeue()) {
worker($job);
# print "$job \n";
}
};
}
$q->enqueue($_) for @file_splitted;
$q->end();
for my $t(@workers){
(my @getit)= $t->join();
my $tmp = join '', @getit;
$result .= $tmp;
print "$result\n";
}
}
This here is your current code, but tidied up a bit, and commented:
The problem is that you aren't actually returning anything useful from your threads – note that I removed the
worker
subroutine above because it doesn't add anything to this discussion, and probably confused you.Quite likely, you will want to create another queue from which the threads can return results:
If you only want to collect all results at the end, you could do something like this instead: