Is it possible to select on the stdout and stderr of a long-running std::process::Child that produces output intermittently?

Basically the title. I don't want to merge the stdout and stderr, I just want to be able to run a function over their buffers and then print them to the host's stdout and stderr, respectively. Is there a stdlib-based "select"-y kind of operation?

Wouldn't this be more like a map than a select? (Where map changes each element in an iterator and select composes concurrency primitives by picking the earliest one to become ready)

As far as I know, you have to spawn a thread to do this.

1 Like

If you're willing to relax stdlib requirement, you could use tokio::process rather than std::process without having to, at least explicitly, spawn a separate thread.

Here's an example code that spawns a child process which writes to stdout and stderr, transforms its output, and then forwards that transformed output to the host stdout and stderr: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3d62c50a718d37ed57852c59ef526ef4

In tokio's default configuration, it will probably launch some work threads. However, this code would fundamentally still work with tokio's single-threaded runtime. It would be on you to make sure that you yield to tokio's runtime every so often so that it can process new messages from the child process.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.