问题
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:
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()
回答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.
来源:https://stackoverflow.com/questions/16986658/python-call-external-program-non-blocking-way