OK. I've got something that works, but it seems necessary to add a dummy
write and then strip it out at the end.

julia> function return_stdout(f::Function)
           stdout_orig = STDOUT
           rd, wr = redirect_stdout()
           print("_")      # dummy write so eof(rd) doesn't block when f()
has no output
           f()
           close(wr)
           eof(rd)
           close(rd)
           out = readall(rd)
           out = replace(out, "_", "", 1)
           redirect_stdout(stdout_orig)
           out
       end
return_stdout (generic function with 1 method)

julia> print("Case 1: "*return_stdout(()->println("Some output")))
Case 1: Some output

julia> print("Case 2: "*return_stdout(()->run(`echo Some output`)))
Case 2: Some output

julia> print("Case 3: "*return_stdout(()->nothing))
Case 3:
julia>

Note that if you comment out the print("_") line then it will block in Case
3. Seems strange to me that this is necessary, but at least it works. Also,
the ordering of the close statements and eof statement is important and not
obvious from the docs. Does this look like a good way to do this?

Thanks,
Bob


On Tue, Jun 3, 2014 at 11:35 AM, Jameson Nash <[email protected]> wrote:

> readall or readbytes
>
> The number of bytes that can be read without a syscall (nb_available) is
> not a good test of how much can be gotten from a syscall. However, after
> eof returns true, then nb_available is the upper bound on the data that can
> be read.
>
>
>
> On Tuesday, June 3, 2014, Bob Nnamtrop <[email protected]> wrote:
>
>> OK, but I still cannot seem to find out if there is output available. Try:
>>
>> julia> function test_redirect(f::Function)
>>            stdout_orig = STDOUT
>>            rd, wr = redirect_stdout()
>>            f()
>>            close(wr)
>>            nb = nb_available(rd)
>>            redirect_stdout(stdout_orig)
>>            nb
>>        end
>> test_redirect (generic function with 1 method)
>>
>> julia> println(test_redirect(()->println("Some output")))
>> 0
>>
>> julia> println(test_redirect(()->run(`echo Some output`)))
>> 0
>>
>> julia> println(test_redirect(()->nothing))
>> 0
>>
>> So whether there is anything there or not nb_available returns 0?
>>
>> What do you recommend instead of readavailable?
>> Bob
>>
>>
>> On Tue, Jun 3, 2014 at 9:00 AM, Jameson Nash <[email protected]> wrote:
>>
>>> 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
>>>>
>>>>
>>>>
>>

Reply via email to