Re: Perl threads - capturing value returned from sub
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 01:21:36 GMT
"Eric" <ecarlson@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
.
- Follow-Ups:
- References:
- Prev by Date: Re: DocumentHTML ?
- Next by Date: Re: Perl threads - capturing value returned from sub
- Previous by thread: Perl threads - capturing value returned from sub
- Next by thread: Re: Perl threads - capturing value returned from sub
- Index(es):
Relevant Pages
|