Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^5: thread save calling an external command

by BrowserUk (Patriarch)
on Jul 06, 2016 at 12:54 UTC ( #1167295=note: print w/replies, xml ) Need Help??

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.

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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-05-21 07:30 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.