Could you please help me to develop this, I have tried the Thread::Queue. It takes too much time to retrieved the result from a source and I have to search more than 50 sources at a time. There could be more than 10 instances that would be running concurrently.
How can main thread can read them off and display>
following is the code I am using
sub run_search {
my ($self, $searches, $search_string, $site, $max_hits, $from_year
+, $to_year) = @_;
my $Qwork = new Thread::Queue;
my $Qresults = new Thread::Queue;
my $THREADS = scalar(keys %$searches);
my @return;
foreach my $obj (values %$searches) {
eval {
$obj->from_year($from_year);
$obj->to_year($to_year);
$obj->parse_search();
};
if ($@) {
print STDERR "problem2 $@\n";
}
$Qwork->enqueue($obj);
}
$Qwork->enqueue( (undef) x $THREADS );
my @pool = map{
threads->create( \¶llel_search, $Qwork, $Qresults, $max_hi
+ts, $self->nuc_code)
}1 .. $THREADS;
for(1..$THREADS){
while( my $result = $Qresults->dequeue ){
push(@return, $result);
}
}
## Clean up the threads
$_->join for @pool;
return(\@return);
}
#
#
# the parallel server
#
#
sub parallel_search {
my ($Qwork, $Qresults, $max_hits, $nuc_code) = @_;
my $tid = threads->tid;
my %result;
while(my $work = $Qwork->dequeue) {
'require ' . ref($work) . ';';
$work->max_hits($max_hits);
$result{$work->resource_id} = $work->get_search_results($work-
+>resource_id, $nuc_code);
$Qresults->enqueue( \%result );
}
$Qresults->enqueue( undef );
}
|