This will start four processes on my 4 core system within 0.2 of a second of each other:
#! perl -sw use strict; use threads; use threads::shared; use Thread::Queue; use Time::HiRes qw[ time ]; our $T //= 4; ## 80 my $sem :shared = 0; my @commands = map qq[perl -MTime::HiRes=time -le"print \$\$, ':', tim +e()"], 1 .. $T; sub doit { my( $cmd, $Q ) = @_; lock $sem; my $pid = open my $pipe, '-|', $cmd or die $!; $Q->enqueue( "$pid:$_" ) while <$pipe>; $Q->enqueue( undef ); } my $Q = new Thread::Queue; { lock $sem; async( \&doit, $_, $Q )->detach for @commands; sleep 2; } open OUT, '>', 'log' or die $!; for( 1 .. $T ) { print OUT while defined( $_ = $Q->dequeue ); } close OUT; __END__ c:\test>type log 25692:25692:1481831121.12942 26080:26080:1481831121.18679 26088:26088:1481831121.2571 25704:25704:1481831121.32448
To get closer than that you'd need to move the semaphoring into the child processes.
In reply to Re: @array_commands to execute simultaneously and collect output on single log
by BrowserUk
in thread @array_commands to execute simultaneously and collect output on single log
by nicopelle
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |