Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: thread save calling an external command

by salva (Canon)
on Jul 06, 2016 at 09:26 UTC ( #1167289=note: print w/replies, xml ) Need Help??

in reply to thread save calling an external command

There is a very simple solution to that problem: Redirect the output of every child process to temporary file and once it terminates, read it back for processing. That way you only have to take care of concurrently launching and reaping processes, forgetting about the IO.

It may seem pretty inefficient forcing the data through the file-system, but in practice, usually it is not unless the slave processes write huge quantities of data.

  • Comment on Re: thread save calling an external command

Replies are listed 'Best First'.
Re^2: thread save calling an external command
by Anonymous Monk on Jul 06, 2016 at 13:00 UTC

    Simple... and neat using File::Temp:

    use File::Temp; sub cmdrun { my $cmd = shift; my $out = File::Temp->new(); my $err = File::Temp->new(); local $/; `$cmd >$out 2>$err`; $?>>8, scalar <$out>, scalar <$err> } use Data::Dumper; print Dumper cmdrun( q(perl -le "warn q(NOISE); print q(OUTPUT); exit +42;") );

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2022-05-19 03:09 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.