subprocess.run()
is a blocking call, which returns stdout after the started program exited.For your task (starting processes) is multiprocessing not needed and not helpful.
Instead of using
subprocess.run
, use subprocess.Popen
which is not a blocking call.It returns an object, where stdout, stderr and stdin is attached to.
from subprocess import Popen, PIPE proc = Popen(['ls', '--help'], stdout=PIPE, stderr=PIPE, encoding='utf8', errors='ignore') # use stdout # use stderr # stdin is not connected to the PIPE, this means no access to stdin # encoding is utf8, which opens stdout, stderr and stdin in text mode. # error are ignored # proc.stdout.read() # blocks until the program finishes (EOF) # proc.stdout.read(10) # blocks until 10 characters are available. # proc.stdout.readline() # blocks until a line is availableYou can also use select to look if data is available.
Handling the streams right with subprocess is not always easy.
If you need to wait for the whole output of the started program and do not want to block, you can start the proc from a Thread.
Attach a queue to the function and put the data into the queue.
On the other side you can use
queue.get_nowait()
on the queue to not to block.The use of queue.get_nowait() need to require catching the exception
queue.Empty
.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!