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.

4

2 回答 2

5

是的,使用subprocess模块。例如:

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()
于 2013-06-07T14:37:14.680 回答
2

忘了os.system()。它已被弃用,取而代之的是subprocessmodule

它为几乎所有可以想到的用例提供了一种执行子程序的方法。

于 2013-06-07T14:37:30.320 回答