Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python call external program non-blocking way

is there a way to call an external program inside python and don't wait for its execution to finish?

I tried this, but no luck:

os.system("external_program &")

Normally, if I call external_program & inside a bash shell it executes as a background process. How can I do it inside python? For, my special case, creating another thread does not work. After main python scrip is done, the external program should continue its execution.

like image 376
Gokhan Ka Avatar asked Mar 23 '23 15:03

Gokhan Ka


1 Answers

Yes, use the subprocess module. For example:

p = subprocess.Popen(['external_program', 'arg1', 'arg2'])
# Process is now running in the background, do other stuff...
...
# Check if process has completed
if p.poll() is not None:
    ...
...
# Wait for process to complete
p.wait()
like image 107
Adam Rosenfield Avatar answered Apr 02 '23 19:04

Adam Rosenfield