Blocking and Non Blocking subprocess calls
I'm completely confused between
Which is blocking and which is not ?
What I mean to say is if I use
subprocess.Popen() whether the parent process waits for the child process to
exit before it keep on its execution.
shell=True affect these calls?
Popen is nonblocking.
check_call are blocking. You can make the
Popen instance block by calling its
If you look in the source code, you'll see
Popen(...).wait(), which is why it is blocking.
call, which is why it blocks as well.
shell=True is orthogonal to the issue of blocking. However,
shell=True causes Python to exec a shell and then run the command in the shell. If you use a blocking call, the call will return when the shell finishes. Since the shell may spawn a subprocess to run the command, the shell may finish before the spawned subprocess. For example,
import subprocess import time proc = subprocess.Popen('ls -lRa /', shell=True) time.sleep(3) proc.terminate() proc.wait()
Here two processes are spawned: Popen spawns one subprocess running the shell. The shell in turn spawns a subprocess running
proc.terminate() kills the shell, but the subprocess running
ls remains. (That is manifested by copious output, even after the python script has ended. Be prepared to kill the
★ Back to homepage or read more recommendations: