Python: how to execute an external program

How to execute a program from my program without blocking until the program is completed?

I tried:

os.system()

But it stops my program before stopping / closing the executable program. Is there a way for my program to continue to work after running an external program?

+3
source share
4 answers

Consider using the subprocess module.

Subprocess

, . , .

+11

You can use the subprocess module, but os.system will also work. It works through the shell, so you just need to put "&". at the end of the line. As in the interactive shell, it will work in the background.

If you need to get any output from it, most likely you will want to use the subprocess module.

+2
source

You can use subprocessfor this:

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
                          )
+1
source

Source: https://habr.com/ru/post/1779660/


All Articles