in reply to Re^4: thread save calling an external command
in thread thread save calling an external command
I have chosen to use backticks while the thread has to wait on the completion of the external command.
I also find backticks simplest for many purposes.
The primary advantage of using the piped-open is that it allows you to inject code -- time out; remote abort etc. -- into the read loop:
my $pid = open CMD, '-|', $command or die $!; my $timeout = time() + 10; ## 10 seconds should be enough my $output = ''; while( <CMD> ) { $output .= $_; last if $sharedFlag == ABORT; kill 9, $Pid, last if time() > $timeout; } close CMD; return $output;
It's not perfect. If the child process stops producing output, it will hang at the read, but it is good for most things.
The next level is to use Win32::SocketPair::open2_5() to obtain a handle that can be set non-blocking and use select or sysread to avoid the hang.
A similar thing can be done directly on *nix.
My conclusion, do not use IPC::Run3 or Capture::Tiny in multi threaded scripts.
I concur. Though I would also go further and remove the qualification; both modules (and all the others attempting to do the same thing) are just so overcomplicated that they create more problems than they attempt (and fail) to solve.
|
|---|