Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

parallel functions

by micmac (Acolyte)
on Nov 07, 2012 at 21:11 UTC ( #1002746=perlquestion: print w/replies, xml ) Need Help??
micmac has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm trying to run two functions in parallel thusly:

#!/usr/bin/perl use 5.014; my @child_pids; for my $cmd (\&test1, \&test2) { defined(my $child_pid = fork()) or die "Couldn't fork: $!"; if ($child_pid == 0) { exec $cmd->(); } else { push @child_pids, $child_pid; } } for my $pid (@child_pids) { waitpid($pid, 0); } sub test1 { sleep 3; say "in test1"; } sub test2 { sleep 2; say "in test2"; }
in test2 in test1 in test2
Say what? Why is it running test2 twice?
thanks for any responses

Replies are listed 'Best First'.
Re: parallel functions
by kcott (Chancellor) on Nov 07, 2012 at 21:58 UTC

    G'day micmac,

    If you add use warnings;, you'll see that the exec is not being run at all:

    $ in test2 Can't exec "1": No such file or directory at ./ line +13. in test1 Can't exec "1": No such file or directory at ./ line +13. in test2 Can't exec "1": No such file or directory at ./ line +13.

    What's actually happening is that $cmd->() is being run then exec is trying to use the return value ("1" from say) as its argument.

    So, the first child runs test1() then, because there's no exec, it runs test2() on the second iteration. The second child runs test2() (also with no exec). That's test1() once and test2() twice.


    exec $cmd->();


    #exec $cmd->(); $cmd->(); last;

    you'll get the desired output:

    $ in test2 in test1

    I'll leave you to decide if that's also the desired functionality.

    -- Ken

      Good explanation, thanks Ken.
Re: parallel functions
by tobyink (Abbot) on Nov 07, 2012 at 21:40 UTC

    Because the first fork creates two processes. One of those processes calls test1, the other does not. Then both processes fork again, creating a total of four processes; two of those processed call test2.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: parallel functions
by chromatic (Archbishop) on Nov 07, 2012 at 21:41 UTC
Re: parallel functions
by Anonymous Monk on Nov 07, 2012 at 21:53 UTC
    I did indeed mean exit.

    Thanks for both responses.

Re: parallel functions
by marioroy (Deacon) on Nov 25, 2012 at 06:26 UTC

    A new parallel module was recently added to CPAN called MCE. This is how one could run 2 functions in parallel using MCE.

    #!/usr/bin/perl use 5.014; use MCE; my $mce = MCE->new( max_workers => 2, user_func => sub { my $self = shift; if ($self->wid == 1) { test1(); } else { test2(); } } ); $mce->run(); sub test1 { sleep 3; say "in test1"; } sub test2 { sleep 2; say "in test2"; }


    in test2 in test1

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002746]
Approved by nemesdani
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2017-06-24 16:31 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (560 votes). Check out past polls.