Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have a script for 4 systems commands, that are executed via the "system" command in Perl. My question is. since the way my script is now, the 4 commands are executed one after the other (since as we know Perl reads the script line-by-line), is there a way to execute ALL commands at the same time?
The commands are independent of each other, it's not like I need to run first command #1, then command #2 etc.
Thanks

Replies are listed 'Best First'.
Re: Run system commands simultaneously
by BrowserUk (Patriarch) on May 28, 2013 at 10:55 UTC

    Yes.

    use threads; ... my $t1 = async{ system '...'; }; my $t2 = async{ system '...'; }; my $t3 = async{ system '...'; }; my $t4 = async{ system '...'; }; $_->join for $t1, $t2, $t3, $t4;

    Or just:

    $_->join for map async( sub{ system shift; }, $_ ), 'command1 args', 'command2 args', 'command3 args', 'command4 args';

    They'll be more to it than that once you get around to describing the full requirements; but on the basis of what you've told us so far, that would do it.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
Re: Run system commands simultaneously
by Anonymous Monk on May 28, 2013 at 10:53 UTC