Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: @array_commands to execute simultaneously and collect output on single log

by BrowserUk (Patriarch)
on Dec 15, 2016 at 19:43 UTC ( [id://1177879]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177879]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-16 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found