in reply to Re: How to share streams between processes
in thread How to share streams between processes

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?

  • Comment on Re^2: How to share streams between processes

Replies are listed 'Best First'.
Re^3: How to share streams between processes
by BrowserUk (Patriarch) on Sep 03, 2008 at 17:44 UTC

    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);