Without the calls to close(rd) and eof(rd) the code above hangs on the
first call (Case 1) at readall. If I use readbytes then I need the number
of bytes which I cannot get without calling eof. If I replace "readall"
with "readavailable" then it works w/o the close or eof calls. This is why
I was using readavailable since it seems simpler to use. In all cases I
need the dummy write.

I really have no idea which is best. It seems overly complicated for
something so basic. And next I'd like to capture the STDERR at the same
time (and it likely will be empty). Is this possible?

Bob


On Tue, Jun 3, 2014 at 1:07 PM, Jameson Nash <[email protected]> wrote:

> You shouldnt need to explicitly call eof or close
>
>
> On Tuesday, June 3, 2014, Bob Nnamtrop <[email protected]> wrote:
>
>> 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 p
>>
>>

Reply via email to