Can subprocess.call be invoked without waiting for process to finish?
Question:
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?
Answers:
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.
Shouldn’t the answer be to use ‘close_fds’ flag?
subprocess.Popen(cmd, stdout=None, stderr=None, stdin=None, close_fds=True)
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?
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.
Shouldn’t the answer be to use ‘close_fds’ flag?
subprocess.Popen(cmd, stdout=None, stderr=None, stdin=None, close_fds=True)