Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can subprocess.call be invoked without waiting for process to finish?

Tags:

python

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?

like image 896
zer0stimulus Avatar asked Jun 10 '12 02:06

zer0stimulus


People also ask

Does subprocess call wait for completion?

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.

What is difference between subprocess Popen and call?

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.

Does subprocess call raise exception?

check_call will raise an exception if the command it's running exits with anything other than 0 as its status.

Is Popen wait blocking?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.


1 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.

like image 72
Blender Avatar answered Oct 12 '22 08:10

Blender