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;") );