I'm currently using subprocess.call() to invoke another program, but it blocks the executing thread until that program finishes. Is there a way to simply launch that program without waiting for return?
The subprocess module provides a function named call. This function allows you to call another program, wait for the command to complete and then return the return code.
Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.
check_call will raise an exception if the command it's running exits with anything other than 0 as its status.
Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.
Use subprocess.Popen
instead of subprocess.call
:
process = subprocess.Popen(['foo', '-b', 'bar'])
subprocess.call
is a wrapper around subprocess.Popen
that calls communicate
to wait for the process to terminate. See also What is the difference between subprocess.popen and subprocess.run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With