in reply to How to share streams between processes

Hm. You want to spawn a thread, execute an external process from within that thread and then leave that thread doing nothing whilst your main thread reads from the process. And once the external process finishes, have the thread terminate.

Why? Why complicate things by having the thread start the process and then do nothing, if you want the main thread to read the data? Forget the thread, it is achieving nothing but complication. Just run the external process from the main thread in any of the normal ways backticks, piped open IPC::open.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re: How to share streams between processes

Replies are listed 'Best First'.
Re^2: How to share streams between processes
by rapide (Beadle) on Sep 03, 2008 at 15:55 UTC
    Well, the idea is that the external process generates data and the main thread reads this data simultaneously. I might be thinking too complicated when considering an empty thread, but how could I do it?

    Some pseudo to illustrate my point:

    -MAIN: Create new external process
    -----> EXTERNAL: <generates data>
    MAIN: <while data is being generated>
    MAIN: do something with it.
    -----> EXTERNAL: <exits>
    MAIN: <no more data>
    MAIN: <exits>

    Q: If you were to do this with sharing the streams "STDOUT" and "STDERR", how would you?

      If you need the output from both stdout and stderr via a single stream:

      #main my $kid = open my $fh, "theExternalProcess 2>&1 |" or die $!; while( <$fh> ) { ## Read the output from theExternalProcess } ## You'll end up here when the kid goes away

      If you need to keep stdout and stderr separate, then see IPC::Open3 or IPC::Run.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Thanks for that reply. I needed to keep them separate but managed in the end to find both a solution and the reason for me to have the trouble in the first place. Seems like I forgot to flush the stream which made it seem the processes did not work in parallel;

        <P> select((select(STDOUT), $|=1)[0]); <P> And the whole script that made it all work: <br> <br>$pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $path, @arguments); <br> close(CMD_IN); <br> <br> $selector = IO::Select->new(); <br> $selector->add(*CMD_ERR, *CMD_OUT); <br> <br> while(@ready = $selector->can_read) { <br> foreach my $fh (@ready) { <br> if(fileno($fh) == fileno(CMD_ERR)) { <br> my $line = scalar <CMD_ERR>; <br> if(defined $line) { <br> parseErrorStream($line); <br> } <br> } <br> else { <br> my $line2 = scalar <CMD_OUT>; <br> if(defined $line2 and $print eq 1) { <br> parseStandardStream($line2); <br> } <br> } <br> $selector->remove($fh) if eof($fh); <br> } <br> } <br> close(CMD_OUT); <br> close(CMD_ERR);