in reply to @array_commands to execute simultaneously and collect output on single log

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.


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". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: @array_commands to execute simultaneously and collect output on single log
  • Download Code