Re: Perl threads - capturing value returned from sub
- From: "Eric" <ecarlson@xxxxxxxxxx>
- Date: 26 Feb 2007 17:27:36 -0800
On Feb 26, 5:21 pm, "A. Sinan Unur" <1...@xxxxxxxxxxxxxxxxxxx> wrote:
"Eric" <ecarl...@xxxxxxxxxx> wrote in news:1172538201.694433.133140
@j27g2000cwj.googlegroups.com:
I am using Perl threads to launch multiple executions of a command in
parallel. The code to do this is:
Please read the posting guidelines for this group. Code you post should
be readily runnable by copying and pasting.
... code snipped ... edited version below
The problem is that I don't seem to be able to figure out how to
capture the return value in the calling routine. Does anyone know how
to do this?
You need to store the returned values in a shared data structure.
I had to write and auxillary script to simulate the external
application.
#!/usr/bin/perl
use strict;
use warnings;
my @retvals = qw( SUCCESS FAILURE SOMETHING_ELSE );
print $retvals[ @retvals * rand ], "\n";
__END__
Then, replace your script with:
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
my %status;
share %status;
my @machines = qw( apple orange banana pineapple cherry );
for my $machine ( @machines ) {
my $t = threads->new( \&action, $machine );
$t->join;
}
use Data::Dumper;
print Dumper \%status;
sub action {
my ($machine) = @_;
my $output = `s.pl`;
if ( $output =~ /SUCCESS/ ) {
$status{ $machine } = 'SUCCESS';
}
elsif ( $output =~ /FAILURE/ ) {
$status{ $machine } = 'FAILURE';
}
else {
$status{ $machine } = 'INTERNAL ERROR';
}
return;
}
__END__
C:\DOCUME~1\asu1\LOCALS~1\Temp\2> r
$VAR1 = {
'cherry' => 'SUCCESS',
'banana' => 'SUCCESS',
'apple' => 'FAILURE',
'orange' => 'INTERNAL ERROR',
'pineapple' => 'FAILURE'
};
--
Sinan
Thanks for your response, Sinan. I'll give it a try.
I didn't want to paste the entire code as I thought it would only add
clutter.
Eric
.
- References:
- Perl threads - capturing value returned from sub
- From: Eric
- Re: Perl threads - capturing value returned from sub
- From: A. Sinan Unur
- Perl threads - capturing value returned from sub
- Prev by Date: Re: Perl threads - capturing value returned from sub
- Next by Date: FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS?
- Previous by thread: Re: Perl threads - capturing value returned from sub
- Next by thread: Re: Perl threads - capturing value returned from sub
- Index(es):
Relevant Pages
|