Non-blocking Read from StdoutPipe and StderrPipe

196 views
Skip to first unread message

Peter Bočan

unread,
Oct 14, 2024, 5:57:07 PM10/14/24
to golang-nuts
Hello Community, 

I am writing a small binary which watches the subprocesses it launches. However, I have noticed that cmd.StdoutPipe and cmd.StderrPipe returns io.ReadCloser/io.File, which also, in itself blocks when reading. 

What is the recommended way of reading whatever is on the buffer and return back without blocking? It looks like a bit of a rabbit hole. Is setting the nonblocking flag using syscall recommended? 

Kindly,
Peter.

robert engels

unread,
Oct 14, 2024, 6:08:51 PM10/14/24
to Peter Bočan, golang-nuts
I am not sure that is idiomatic Go. Start a new Go routine to do the reading, and post the results to a channel that the main routine selects on.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/71f46bd4-0916-4e1b-8821-17d884d0333an%40googlegroups.com.

Nico Braun

unread,
Oct 14, 2024, 6:56:39 PM10/14/24
to golang-nuts
Perhaps you can use a context. It wouldnt be truely nonblocking but maybe it is sufficient to use cancel or deadline. https://cs.opensource.google/go/go/+/refs/tags/go1.23.2:src/os/exec/exec.go;drc=d0146bd85bb6870aa43a498b06ccb473af55cbe3;l=453.

Brian Candler

unread,
Oct 15, 2024, 5:12:15 AM10/15/24
to golang-nuts
On Monday 14 October 2024 at 22:57:07 UTC+1 Peter Bočan wrote:
What is the recommended way of reading whatever is on the buffer and return back without blocking?

If you just want to poll, and get whatever data is available: set a deadline of "just after now", and read.
https://go.dev/play/p/bFDh7gqpeYc   [Note: doesn't work properly in the playground; I don't know why]

If you want to do other concurrent work and/or select between multiple sources: use a goroutine to read and pass the data down a channel, and closes the channel when done.

Peter Bočan

unread,
Oct 16, 2024, 3:59:56 PM10/16/24
to golang-nuts
Hey peeps, 

thank you for your input, I will try these options and see how it goes. 

Regards,
Peter.
Reply all
Reply to author
Forward
0 new messages