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 | |
by BrowserUk (Patriarch) on Jul 06, 2016 at 12:54 UTC |
In Section
Seekers of Perl Wisdom