# How to read from a process stdout without it blocking?

**URL:** <https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [September 4, 2024, 3:31pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880 "2024-09-04T15:31:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 4, 2024, 3:31pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/1 "2024-09-04T15:31:26Z")

</div>

Ive launched a process and set its output mode to .Pipe. Now I want to read whatever was printed without it blocking. How do I do that?

Context: I am trying to make a gui that calls to a cli whose output I want to show in realtime.

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 4, 2024, 7:34pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/2 "2024-09-04T19:34:16Z")

</div>

There are several approaches to non-blocking I/O. What you probably want to do is to use `poll` or `select` to get notified when the input is ready to read or after a timeout. [`std.io.poll`](https://ziglang.org/documentation/master/std/#std.io.poll) will work here.

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 4, 2024, 7:36pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/3 "2024-09-04T19:36:07Z")

</div>

Huh okay I knew std.io.poll existed, I just thought that it wouldnt work here for some reason, and hoped that if it did that there would be a nicer way.

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 4, 2024, 7:42pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/4 "2024-09-04T19:42:07Z")

</div>

Non-blocking I/O in low-level languages is a bit of a pain in the ass both because blocking I/O is simpler and because there is a wealth of incompatible APIs:

- basically everywhere: select, poll
- linux: epoll, /dev/poll, io\_uring
- bsds: kqueue
- windows: io completion ports
- userspace abstractions: aio, libuv, libev, libevent, many many more

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 4, 2024, 7:50pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/5 "2024-09-04T19:50:56Z")

</div>

Yea Im not too familiar with the topic, but i have heard some of those words and the sheer amount of them scared me off every digging deeper regarding that topic.

I was hoping that someone would inform me about the forgotten child, something like a method of file that just does something like [this](https://codeberg.org/segfault-enjoyer/tcp-chat/src/branch/main/src/server_utils.zig#L3) (this was from a school project where my glass used java and my teacher let me use zig), just in a simpler way.

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 4, 2024, 7:52pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/6 "2024-09-04T19:52:01Z")

</div>

Speaking of which: Why is poller such a complex struct instead of just a function? Is it a code smell to use it the way I did there? Why the ability to poll multiple files with just one poller?

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 4, 2024, 8:02pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/7 "2024-09-04T20:02:32Z")

</div>

> Why is poller such a complex struct instead of just a function?

I believe it’s mainly for convenience.

> Is it a code smell to use it the way I did there?

I don’t think so, for your application it seems fine.

> Why the ability to poll multiple files with just one poller?

Many applications [like servers](http://kegel.com/c10k.html) need to be notified of many file descriptors.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [September 4, 2024, 8:36pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/8 "2024-09-04T20:36:51Z")

</div>

Just an addition. “Non-blocking” I/O in literal sense is when you set `O_NONBLOCK` flag for a file descriptor (socket for ex.) and then do `read()` or `write()` in a loop (with optional small pause), checking if `errno` is `EWOULDBLOCK/EAGAIN` in case when these calls return `-1`. Of course, this is a CPU hog, but such an approach is quite applicable when you know for sure that data is almost always available.

Also `O_NONBLOCK` is often set even when using `epoll` and alike because there may be “spurious” `POLLIN` notifications. To deal with them you

- either set `O_NONBLOCK``and check for `EAGAIN` as when doing “pure” non-blocking i/o
- or determine how much data is available (`ioctl(FIONREAD)`) and if it is zero, do not perform a `read()`

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 4, 2024, 9:14pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/9 "2024-09-04T21:14:55Z")

</div>

You are technically correct! The best kind of correct!

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 5, 2024, 5:41am UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/10 "2024-09-05T05:41:36Z")

</div>

How do I do the O\_NONBLOCK thing? Im pretty sure ive done it before but just dont know how and where.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [September 5, 2024, 6:22am UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/11 "2024-09-05T06:22:11Z")

</div>

Something like [this](https://github.com/dee0xeed/xjiss/blob/main/xjiss2/src/engine/event-sources.zig#L304). There non-blocking mode is used for asynchronous tcp-connect.

- get flags via `fcntl(F_GETFL)`
- add NONBLOCK with `|=` to them
- set flags via `fcntl(F_SETFL)`

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 5, 2024, 6:27am UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/12 "2024-09-05T06:27:23Z")

</div>

Alright thank you!

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 5, 2024, 6:37am UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/13 "2024-09-05T06:37:17Z")

</div>

Wait theres no way this is crossplatform is there?

No it cant be.

I need this to be crossplatform (windows and linux)

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [September 5, 2024, 7:59am UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/14 "2024-09-05T07:59:41Z")

</div>

> [@markus](#):
>
> I need this to be crossplatform (windows and linux)

Then `select()` is probably your choice (see also [this comment](https://ziggit.dev/t/iofthetiger/3065/51)). But I am not sure if this is really good advice.

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [September 5, 2024, 7:58pm UTC](https://ziggit.dev/t/how-to-read-from-a-process-stdout-without-it-blocking/5880/15 "2024-09-05T19:58:21Z")

</div>

I ended up just using std.io.poll which to my surprise actually worked.

It surprised me because just calling read on the stream just blocked until the execution of the subtask was fully over, I thought that this would mean that polling for input would also just skip until the subtask is fully done, leading to non-realtime output. TIL: it does give me realtime output.
