I got my answer.
instead of
my $id = Thread::Pool->jobid;
add
my $id = threads->tid();
And its working correctly..
Domanda
I am not able to get job id inside do subroutine for Thread::Pool in Perl. Below is the code. Can anybody please help me?
use strict;
use warnings;
use Thread::Pool;
sub PrintNumbers( $ )
{
my $num = shift;
my $id = Thread::Pool->jobid;
print "$id => $num\n";
}
my $pool = Thread::Pool->new(
{
optimize => 'cpu',
do => \&PrintNumbers,
workers => 2,
maxjobs => 4
}
);
$pool->job( $_ ) foreach ( 1..100 );
$pool->shutdown;
Soluzione
I got my answer.
instead of
my $id = Thread::Pool->jobid;
add
my $id = threads->tid();
And its working correctly..
Altri suggerimenti
Job ids are useless if the job creator doesn't keep them, so they self-destruct when the job creator discards them.
If you need the job ids, you're going to have to save them, and doing so solves your problem.
my %job_ids;
$job_ids{$_} = $pool->job($_) for 'a'..'z';
Output:
1 => a
2 => b
3 => c
...