in reply to Parallel Processing in Perl

See threads.

Simplest solution:

use threads; ... while(1) { async( \&sub1, $hash1 )->detach; async( \&sub2, $hash2 )->detach; sleep(600); }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: Parallel Processing in Perl
by shree (Acolyte) on Mar 27, 2010 at 18:25 UTC
    Here I want to access get the values returned values from the subs, by using threads I have wrote the code as below:
    use threads; use Data::Dumper; my $hash1; my $hash2; while(1) { async( \&sub1)->detach; async( \&sub2)->detach; print '$hash1'. Dumper($hash1) ."\n"; print '$hash2'. Dumper($hash2) ."\n"; print "_______________________________\n"; sleep(10); } sub sub1 { my $now = `date "+%Y-%m-%d %H:%M:%S"`; chomp($now); $hash1->{'time'} = $now; } sub sub2 { my $now = `date "+%Y-%m-%d %H:%M:%S"`; chomp($now); $hash2->{'time'} = $now; }
    But when I execute this program getting the values like:
    $hash1$VAR1 = undef;
    $hash2$VAR1 = undef;
    _______________________________
    Here I want to get the values of both hash in Main program;
    how can I do this..
    Thanks

      There are several ways of achieving your goal. I cannot infer which is appropriate from your sample code.

      Here are two ways:

      #! perl -sw use strict; use threads; use threads::shared; use Data::Dumper; my %hash1 :shared; my %hash2 :shared; while(1) { async( \&sub1)->detach; async( \&sub2)->detach; sleep(2); lock %hash1; lock %hash2; print '$hash1'. Dumper(\%hash1) ."\n"; print '$hash2'. Dumper(\%hash2) ."\n"; print "_______________________________\n"; } sub sub1 { my $now = scalar localtime; lock %hash1; $hash1{'time'} = $now; } sub sub2 { my $now = scalar localtime; lock %hash2; $hash2{'time'} = $now; }
      #! perl -sw use strict; use threads; use Data::Dumper; my %hash1; my %hash2; while(1) { my( $t1 ) = threads->create( \&sub1 ); my( $t2 ) = threads->create( \&sub2 ); sleep(2); %hash1 = $t1->join; %hash2 = $t2->join; print '$hash1'. Dumper(\%hash1) ."\n"; print '$hash2'. Dumper(\%hash2) ."\n"; print "_______________________________\n"; } sub sub1 { my $now = scalar localtime; return time => $now; } sub sub2 { my $now = scalar localtime; return time => $now; }

      Try them, note the differences, and ask questions.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Hi BrowserUk,
        Yes It worked for me and its fulfilled my requirements;

        -Thanks a lot
        Shree
        Hi BrowserUk,

        Thanks for your quick and kind response. Yes it totally fulfilled my requirement.

        -Thanks a lot...
        Shree