Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: thread save calling an external command

by BrowserUk (Patriarch)
on Jun 24, 2016 at 10:05 UTC ( #1166478=note: print w/replies, xml ) Need Help??


in reply to Re^2: thread save calling an external command
in thread thread save calling an external command

If you want both stdout and stderr (and don't mind them mixed together:

#! perl -slw use strict; use threads; sub thread { my $output = ''; open CMD, '-|', q[ perl -E"say( qq[$$:saying $_] ), warn( qq[$$:wa +rning $_\n] ), sleep(1) for 1 .. 4" 2>&1 ] or die $!; $output .= $_ while <CMD>; close CMD; return $output; } printf "Got\n'%s'\n", $_->join for map threads->create( \&thread ), 1 +.. 4; __END__ C:\test>t-junk Got '33168:warning 1 33168:warning 2 33168:warning 3 33168:warning 4 33168:saying 1 33168:saying 2 33168:saying 3 33168:saying 4 ' Got '30208:warning 1 30208:warning 2 30208:warning 3 30208:warning 4 30208:saying 1 30208:saying 2 30208:saying 3 30208:saying 4 ' Got '15844:warning 1 15844:warning 2 15844:warning 3 15844:warning 4 15844:saying 1 15844:saying 2 15844:saying 3 15844:saying 4 ' Got '15032:warning 1 15032:warning 2 15032:warning 3 15032:warning 4 15032:saying 1 15032:saying 2 15032:saying 3 15032:saying 4 '

If you only want stderr:

#! perl -slw use strict; use threads; sub thread { my $output = ''; open CMD, '-|', q[ perl -E"say( qq[$$:saying $_] ), warn( qq[$$:wa +rning $_\n] ), sleep(1) for 1 .. 4" 2>&1 >nul ] or die $!; $output .= $_ while <CMD>; close CMD; return $output; } printf "Got\n'%s'\n", $_->join for map threads->create( \&thread ), 1 +.. 4; __END__ C:\test>t-junk Got '34456:warning 1 34456:warning 2 34456:warning 3 34456:warning 4 ' Got '32700:warning 1 32700:warning 2 32700:warning 3 32700:warning 4 ' Got '19172:warning 1 19172:warning 2 19172:warning 3 19172:warning 4 ' Got '22176:warning 1 22176:warning 2 22176:warning 3 22176:warning 4 '

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. Not understood.

Replies are listed 'Best First'.
Re^4: thread save calling an external command
by Paul.Unix (Novice) on Jul 06, 2016 at 09:17 UTC
    Thanks for this example. It got me going. I have chosen to use backticks while the thread has to wait on the completion of the external command. My conclusion, do not use IPC::Run3 or Capture::Tiny in multi threaded scripts.
      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?
Username:
Password:

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

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



    Results (71 votes). Check out past polls.

    Notices?