3

How do I execute a program from within my program without blocking until the executed program finishes?

I have tried:

os.system() 

But it stops my program till the executed program is stopped/closed. Is there a way to allow my program to keep running after the execution of the external program?

4 Answers 4

12

Consider using the subprocess module.

subprocess spawns a new process in which your external application is run. Your application continues execution while the other application runs.

Sign up to request clarification or add additional context in comments.

2 Comments

Unless you explicitly wait for it.
os.system spawns a new process as well - subprocess just gives you more control over it.
4

You want subprocess.

Comments

2

You could use the subprocess module, but the os.system will also work. It works through a shell, so you just have to put an '&' at the end of your string. Just like in an interactive shell, it will then run in the background.

If you need to get some kind of output from it, however, you will most likely want to use the subprocess module.

Comments

1

You can use subprocess for that:

import subprocess import codecs # start 'yourexecutable' with some parameters # and throw the output away with codecs.open(os.devnull, 'wb', encoding='utf8') as devnull: subprocess.check_call(["yourexecutable", "-param", "value"], stdout=devnull, stderr=subprocess.STDOUT ) 

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.