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

Hi there,
I need your help in executing a "system" command as a parallel processing.
Ex:
for 1..5{ system("DoThis.exe"); }

I want all five DoThis.exe's triggered parallely. Thanks in advance

Replies are listed 'Best First'.
Re: Parallel Processing
by moritz (Cardinal) on Jun 07, 2010 at 10:21 UTC
    Then launch DoThis.exe in the background. How this works depends on the operating system. perlport talks a bit about this (if I remember correctly), and Super Search will surely find some discussions about it.

      Thanks every one.

      I found a simple solution that meets my requirement. What I did was added "START" inside the system command
      for 1..5{ system("START DoThis.exe"); }

        No need for START. This will do the trick on Windows:

        for (1..5) { system(1, "DoThis.exe"); }
        See perlport.

      Thanks for the quick response. FYI Script runs in Windows.
Re: Parallel Processing
by Khen1950fx (Canon) on Jun 07, 2010 at 10:43 UTC
    How about this. It uses Parallel::Runner:
    #!/usr/bin/perl use strict; use warnings; use Parallel::Runner; my $runner = Parallel::Runner->new(5); $runner->run( sub { system("corelist CGI") }); $runner->run( sub { system("corelist CGI::Carp") }); $runner->run( sub { system("corelist Net::FTP") }); $runner->run( sub { system("corelist Net::SMTP") }); $runner->run( sub { system("corelist DBI") }); $runner->finish;
Re: Parallel Processing
by bobr (Monk) on Jun 07, 2010 at 11:28 UTC
    Hello,

    you can run system on threads like this:

    use threads; my @thr = (); for my $i (1..5) { push @thr, threads->create('run_command'); } for my $t (@thr) { $t->join } sub run_command { system("DoThis.exe"); }

    Working with 5.10 on Windows.

    -- Roman

Re: Parallel Processing
by doug (Pilgrim) on Jun 07, 2010 at 14:39 UTC

    You've already been given all the fancy ways, but I don't see where anyone has shown you the old-fashioned, primitive way.

    foreach ( 1 .. 5 ) { my $pid = fork() // die "fork() failed: $!"; if ( $pid ) { say "parent just spawned child $pid"; } else { exec "DoThis.exe"; } }

    Of course you'll need to set up a signal handler to catch the SIGCHLD events if you want to know when they're all done.

    - doug