Piping stdout from a ChildProcess while it's running
Is it possible in Deno to read from a ChildProcess's stdout while it is running (before it has completed)?
I'm using
If I then try reading fromIf I try reading direct from
(my bad, this was called from later calling
deno.Command
and .spawn()
to create a ChildProcess
.If I then try reading from
process.stdout.getReader()
it does not return anything until the process has terminated and an output status is returned.
process.stdout
I get the printed warning "Can't collect output because stdout is locked".output()
instead of awaiting status
)
Is it possible to read from a ChildProcess stdout while it is interactively running?5 Replies
Yes, this is the exact scenario that is supported. Do you have a cod Ethan doesn't work as expected that you can share?
So the following program prints
"1\n"
, waits 3 seconds, then prints "2\n"
:
But if I run the following in deno I get no output for 3 seconds, then all 4 bytes at once when the program terminates:
A simpler example would also be:
This also waits 3 seconds, and then reads all 4 bytes at once:
In all cases nothing from stdout is readable until the program has terminated.Your C program is buffering the output - it's only flushed when your program finishes
If you add
setbuf(stdout, NULL);
in the first line of main()
your Deno program behaves as you expectRight you are. That does fix it.
However, this is not necessary when running tasks in the terminal, which does display each line separately.
Is it possible to duplicate this behaviour in Deno?
hmm.. I guess making deno emulate/present as a TTY so that programs buffer less is a different question.
Thanks, you've addressed everything here!
Sounds good! Happy to help