You need to close the pipe you called stdin before trying to read from stdout. Otherwise, the answers you get are correct -- readavailable can return whatever it wants. It is not know if all data written to the pipe is available until the write end is closed, and the read end sees an eof
readavailable is a badly defined function. Don't use it. eof needs to block if there is no data available since it can't answer that question until the stream is closed (eof) or there is more data (!eof) On Tuesday, June 3, 2014, Bob Nnamtrop <[email protected]> wrote: > I am having an issue detecting if STDOUT is empty after I redirect it. I'm > using an external C program may or may not write to STDOUT. Thus, I need to > know if there is anything there before I readavailable from it (since > readavailable blocks if the pipe is empty). Here is some code to show the > issue (I redirect STDOUT and send nothing to it and try to check if there > is anything there): > > julia> stdout_orig = STDOUT > TTY(open, 0 bytes waiting) > > julia> stdout, stdin = redirect_stdout() > (Pipe(open, 0 bytes waiting),Pipe(open, 0 bytes waiting)) > > julia> if !eof(stdout) > out = readavailable(stdout) > end > > This last command blocks on eof(stdout), which makes it worthless for this > purpose. Is it suppose to block when there is nothing in the Pipe? The help > for eof says "If the stream is not yet exhausted, this function will block > to wait for more data if necessary, and then return "false". " I'm not sure > how to interpret this. The behavior of eof seems like a bug to me. > > So looking at the docs I figure I can use nb_available instead but it > doesn't seem to be reliable when the pipe is used by external programs. For > example (restarting julia): > > julia> stdout, stdin = redirect_stdout() > (Pipe(open, 0 bytes waiting),Pipe(open, 0 bytes waiting)) > > julia> stdout, stdin = redirect_stdout() > (Pipe(open, 0 bytes waiting),Pipe(open, 0 bytes waiting)) > > julia> run(`echo Some output`) > > julia> nb_available(stdout) > 0 > > This should have returned 12. Adding flush_cstdio() doesn't help. > nb_available seems to work if you use julia programs to write to STDOUT but > not for external programs. Is this a bug? > > Am I missing something? > > Thanks, > Bob > > ps Here is my version: > > julia> versioninfo() > Julia Version 0.3.0-prerelease+3407 > Commit 152b3d3 (2014-06-02 21:29 UTC) > Platform Info: > System: Darwin (x86_64-apple-darwin13.2.0) > CPU: Intel(R) Core(TM)2 Duo CPU T9550 @ 2.66GHz > WORD_SIZE: 64 > BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY) > LAPACK: libopenblas > LIBM: libopenlibm > > >
