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?

Michael Mrozek
177k29 gold badges172 silver badges179 bronze badges
asked Dec 10, 2010 at 20:08

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.

Eric Wilson
59.8k82 gold badges208 silver badges272 bronze badges
answered Dec 10, 2010 at 20:12
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.

answered Dec 10, 2010 at 20:10

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.

answered Dec 10, 2010 at 21:30

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
 )
answered Sep 18, 2017 at 14:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.