Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl threads - capturing value returned from sub

Reply
Thread Tools

Perl threads - capturing value returned from sub

 
 
Eric
Guest
Posts: n/a
 
      02-27-2007
Hello,

I am using Perl threads to launch multiple executions of a command in
parallel. The code to do this is:

foreach my $Machine ($self->Machines) {
my $thr = threads->new(\&doPowerAction, $Machine);
}
foreach my $t (threads->list()) {
$t->join;
}

The sub referenced is:

sub doPowerAction
{
my $machine = shift;

my $runScript = `power.exp $machine`;

if ($runScript =~ m/SUCCESS/) {
return "SUCCESS";
} elsif ($runScript =~ m/FAILURE/) {
return "FAILURE";
} else {
return "INTERNAL_ERROR";
}
}

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?

Thanks in advance to all that respond.

Eric

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-27-2007
"Eric" <(E-Mail Removed)> 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:umper;
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
 
Reply With Quote
 
 
 
 
Eric
Guest
Posts: n/a
 
      02-27-2007
On Feb 26, 5:03 pm, "Eric" <(E-Mail Removed)> wrote:
> Hello,
>
> I am using Perl threads to launch multiple executions of a command in
> parallel. The code to do this is:
>
> foreach my $Machine ($self->Machines) {
> my $thr = threads->new(\&doPowerAction, $Machine);
> }
> foreach my $t (threads->list()) {
> $t->join;
> }
>
> The sub referenced is:
>
> sub doPowerAction
> {
> my $machine = shift;
>
> my $runScript = `power.exp $machine`;
>
> if ($runScript =~ m/SUCCESS/) {
> return "SUCCESS";
> } elsif ($runScript =~ m/FAILURE/) {
> return "FAILURE";
> } else {
> return "INTERNAL_ERROR";
> }
>
> }
>
> 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?
>
> Thanks in advance to all that respond.
>
> Eric


Cancel this one; I found the answer, which was to just assign a
variable to the join:

my $result = $t->join;

Seems like I always have a breakthrough immediately after I post to
this site!

Eric

 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      02-27-2007
On Feb 26, 5:21 pm, "A. Sinan Unur" <(E-Mail Removed)> wrote:
> "Eric" <(E-Mail Removed)> 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:umper;
> 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

 
Reply With Quote
 
zentara
Guest
Posts: n/a
 
      02-27-2007
On 26 Feb 2007 17:24:40 -0800, "Eric" <(E-Mail Removed)> wrote:


>
>Cancel this one; I found the answer, which was to just assign a
>variable to the join:
>
>my $result = $t->join;
>
>Seems like I always have a breakthrough immediately after I post to
>this site!
>
>Eric


Just to amplify slightly, you can return arrays too.
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;

# join() does three things: it waits for a thread to exit,
# cleans up after it, and returns any data the thread may
# have produced.

my $thr = threads->new(\&sub1);

my $return = $thr->join;

print "Thread returned @$return\n";

#hold for key input
<>;
################################################## ########
sub sub1 {
my @values = ('1',2, 'ten');
print "@values\n";

while(1){sleep 1}

return \@values;
}

__END__


--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 7:21 am, zentara <(E-Mail Removed)> wrote:
> On 26 Feb 2007 17:24:40 -0800, "Eric" <(E-Mail Removed)> wrote:
>
>
>
> >Cancel this one; I found the answer, which was to just assign a
> >variable to the join:

>
> >my $result = $t->join;

>
> >Seems like I always have a breakthrough immediately after I post to
> >this site!

>
> >Eric

>
> Just to amplify slightly, you can return arrays too.
> #!/usr/bin/perl
> use warnings;
> use strict;
> use threads;
> use threads::shared;
>
> # join() does three things: it waits for a thread to exit,
> # cleans up after it, and returns any data the thread may
> # have produced.
>
> my $thr = threads->new(\&sub1);
>
> my $return = $thr->join;
>
> print "Thread returned @$return\n";
>
> #hold for key input
> <>;
> ################################################## ########
> sub sub1 {
> my @values = ('1',2, 'ten');
> print "@values\n";
>
> while(1){sleep 1}
>
> return \@values;
>
> }
>
> __END__
>
> --
> I'm not really a human, but I play one on earth.http://zentara.net/japh.html


Thanks - this is useful information.

Eric

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      02-27-2007
Eric <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> On Feb 26, 5:03 pm, "Eric" <(E-Mail Removed)> wrote:


> Cancel this one; I found the answer, which was to just assign a
> variable to the join:
>
> my $result = $t->join;


What you really do is, you assign the value of ->join to a variable.

This may sound like nit-picking, but taken literally (not just as
a slip of the fingers) your description would reveal a fundamental
misunderstanding of the meaning of "assign" in this usage.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
How to retrieve the Perl script returned value in C program? Many thanks mike Perl Misc 3 05-01-2007 08:27 PM
Value returned from Perl or Javascript? amerar@iwc.net Javascript 3 01-20-2006 08:50 PM
How do I fix "Argument not specified for parameter 'value' of 'Public Overridable Sub Add(key As Object, value As Object)" Chuck Insight ASP .Net Web Controls 2 03-19-2005 12:03 AM



Advertisments