43

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?

juliomalegria
  • 22,918
  • 12
  • 67
  • 88
zer0stimulus
  • 20,576
  • 30
  • 105
  • 139
  • possible duplicate of [Run Process and Don't Wait](http://stackoverflow.com/questions/3516007/run-process-and-dont-wait) – Ned Batchelder Jun 10 '12 at 02:23
  • [Run Process and Don't Wait](http://stackoverflow.com/questions/3516007/run-process-and-dont-wait) is windows specific. – eavsteen Mar 07 '19 at 05:07

2 Answers2

68

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.

idbrii
  • 10,113
  • 5
  • 57
  • 100
Blender
  • 275,078
  • 51
  • 420
  • 480
  • Is this answer supposed to be "no"? This answer does not really answer the question because sometimes you can not simply replace `subprocess.call` by `subprocess.popen` – anion May 04 '21 at 20:28
  • @anion: Correct, the answer is no because the point of `call` is to wait for the process to terminate. – idbrii Jul 21 '21 at 16:50
1

Shouldn't the answer be to use 'close_fds' flag?

subprocess.Popen(cmd, stdout=None, stderr=None, stdin=None, close_fds=True)
PanDe
  • 641
  • 9
  • 20
  • 1
    The explicit argument is not needed, because [`close_fds=True` is default for `Popen`](https://docs.python.org/3/library/subprocess.html?highlight=close_fds#subprocess.Popen) – hc_dev Feb 01 '22 at 12:59