note
Dominus
<blockquote><i>
The major missing feature is that at present there's no way to get it to run <tt>cmd -x arg1</tt>, <tt>cmd -x arg2</tt>...; there's no way to get the constant <tt>-x</tt> in there.
</i></blockquote>
I tried fixing it by changing:
<code>
my $cmd = shift;
</code>
to:
<code>
my @cmd = split /\s+/, shift;
</code>
and:
<code>
$pid{spawn($cmd, split /\s+/, shift @ARGV)} = 1;
</code>
to:
<code>
$pid{spawn(@cmd, shift @ARGV)} = 1;
</code>
(I also decided that splitting the remaining arguments was a fairly dumb mistake, so got rid of that.)<p>
Now, to get it to run <tt>cmd -x arg1</tt>, <tt>cmd -x arg2</tt>...; you just <tt>runN "cmd -x" arg1 arg2</tt>... .<p>
I think this has worked out pretty well in practice so far, but only time will tell. <p>
Complete code is now:<p>
<code>
#!/usr/bin/perl
use Getopt::Std;
my %opt = (n => 1);
getopts('r:n:v', \%opt) or usage();
my @cmd = split /\s+/, shift;
@ARGV = shuffle(@ARGV) if $opt{r};
my %pid;
while (@ARGV) {
if (keys(%pid) < $opt{n}) {
$pid{spawn(@cmd, 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 @_;
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 (unimpl.)
-v: verbose mode
";
exit 1;
}
</code>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-3737">
<p>
--<br><font size="-2">
<a href="mailto:mjd-www-perlmonks+@plover.com">Mark Dominus</a><br>
<a href="http://perl.plover.com">Perl Paraphernalia</a><br></font>
</div></div>
651902
651934