I want to run many processes in parallel with ability to take stdout in any time. How should I do it? Do I need to run thread for each subprocess.Popen() call, a what?
I want to run many processes in parallel with ability to take stdout in
Share
You can do it in a single thread.
Suppose you have a script that prints lines at random times:
And you’d like to collect the output as soon as it becomes available, you could use
selecton POSIX systems as @zigg suggested:A more portable solution (that should work on Windows, Linux, OSX) can use reader threads for each process, see Non-blocking read on a subprocess.PIPE in python.
Here’s
os.pipe()-based solution that works on Unix and Windows: