Here is a version using Text::ParseWords and a modified spawn subroutine, which uses exec in scalar context. This allows parameterized command execution.
#!/usr/bin/perl use Getopt::Std; use Text::ParseWords; my %opt = (n => 1); getopts('r:n:v', \%opt) or usage(); my $cmd = shift; @ARGV = shuffle(@ARGV) if $opt{r}; my %pid; while (@ARGV) { if (keys(%pid) < $opt{n}) { $pid{spawn($cmd, map { shellwords($_) } shift @ARGV)} = 1; } else { delete $pid{wait()}; } } 1 while wait() >= 0; sub spawn { my $pid = fork; die "fork: $!" unless defined $pid; return $pid if $pid; warn "@_\n" if $opt{v}; exec qq(@_); die "exec: $!"; } sub usage { print STDERR "Usage: $0 [-n N] [-r] [-v] command arg1 arg2... Run command arg1, command arg2, etc., concurrently. Run no more than N processes simultaneously (default 1) -r: run commands in random order instead of specified order (unimp +l.) -v: verbose mode "; exit 1; }
The following snippet will ping google.com and amazon.com once in the first chunk and yahoo.com in the second.
runN -v -n 2 'ping -n 1' google.com amazon.com yahoo.com ping -n 1 google.com ping -n 1 amazon.com Ping google.com [72.14.207.99] mit 32 Bytes Daten: Antwort von 72.14.207.99: Bytes=32 Zeit=116ms TTL=242 Ping-Statistik für 72.14.207.99: Pakete: Gesendet = 1, Empfangen = 1, Verloren = 0 (0% Verlust), Ca. Zeitangaben in Millisek.: Minimum = 116ms, Maximum = 116ms, Mittelwert = 116ms Ping amazon.com [72.21.210.11] mit 32 Bytes Daten: ping -n 1 yahoo.com Ping yahoo.com [66.94.234.13] mit 32 Bytes Daten: Antwort von 66.94.234.13: Bytes=32 Zeit=185ms TTL=54 Ping-Statistik für 66.94.234.13: Pakete: Gesendet = 1, Empfangen = 1, Verloren = 0 (0% Verlust), Ca. Zeitangaben in Millisek.: Minimum = 185ms, Maximum = 185ms, Mittelwert = 185ms Zeitüberschreitung der Anforderung. Ping-Statistik für 72.21.210.11: Pakete: Gesendet = 1, Empfangen = 0, Verloren = 1 (100% Verlust)

print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

In reply to Re^3: Parallelization of heterogenous (runs itself Fortran executables) code by codeacrobat
in thread Parallelization of heterogenous (runs itself Fortran executables) code by Jochen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.