Reading from either stdout or stderr of spawned process

circonflexe
Thu, 03 Apr 2025 21:38:26 +0000
General Usage

My code runs an external process (in this case, Pari/GP) which might produce output on either its stdout or stderr file descriptors. I communicate with the process in the following way:

pin, pout = Pipe(), Pipe()
process = run(pipeline(`gp -q -D colors=no`; stdin=pin, stdout=pout); wait=false)
println(pin, "2+2")
readline(pout) # returns "4"

However, I also want to read the stderr messages from the process. If I append stderr=pout to the call above, I get them in the same Pipe object as the stdout messages, which is not desirable.

In C I would use the select(2) system call for this: given a set of FDs, it waits until one of these has data ready (and of course says which one(s) it is). I don’t see anything equivalent in the IO package, but perhaps I missed something? Is there another way to do I/O multiplexing?

4 posts - 3 participants