48

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
  • 24,229
  • 14
  • 73
  • 89
zer0stimulus
  • 22,306
  • 30
  • 110
  • 141
  • 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
  • 1
    [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

77

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,975
  • 5
  • 66
  • 107
Blender
  • 289,723
  • 53
  • 439
  • 496
  • 1
    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
  • 831
  • 10
  • 21
  • 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