4

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.

1
  • Can you run it in parallel? There are no dependencies? Commented Nov 9, 2019 at 0:20

2 Answers 2

5

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() 
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this. It is still blocking. And, if it works, what happens if I don't wait at the end? Is it going to be killed?
I realized that it actually works but the main script waits at the end. I need a complete independence from the main script. Is it possible?
@GokhanKa: If you don't want the main script to wait, then don't call wait(), it's that simple. If you exit while the subprocess is still running, it will continue to run in the background.
I'm not calling wait(). It does not even execute sys.exit(0) after Popen call. If I call p.terminate(), it immediately terminates the subprocess and exits. How strange is this?
Coming from a duplicate, I want to emphasize: replace os.system("thing &") with p = subprocess.Popen(['thing']) without the & (and without the shell) and do whatever else you want to do. p will exist and be running in the background because you created a subprocess, just like the shell does with & and you can eventually reap it with p.wait(). Things will be a little more complex and subtle if thing blocks because it is writing to a pipe or something, but there are other questions which explain this in more detail.
2

Forget about os.system(). It is deprecated in favour of the subprocess module.

It provides a way to execute subprograms for almost every thinkable use case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.