Is there an easy way of gathering the output of a subprocess without actually waiting for it?
I can think of creating a subprocess.Popen()
with capturing its stdout, then call p.communicate()
, but that would block until the subprocess terminates.
I can think of using subprocess.check_output()
or similar, but that also would block.
I need something which I can start, then do other stuff, then check the subprocess for being terminated, and in case it is, takes its output.
I can think of two rather complicated ways to achieve this:
- Redirect the output into a file, then after termination I can read the output from that file.
- Implement and start a handler thread(!) which constantly tries to read data from the stdout of the subprocess and adds it to a buffer.
The first one needs temporary files and disk I/O which I do not really like in my case. The second one means implementing quite a bit.
I guess there might be a simpler way I couldn't think of yet, or a ready-to-be-used solution in some library I didn't find yet.