python主程序调用,python调用外部程序的非阻塞方式

is there a way to call an external program inside python and dont 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.

解决方案

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()

is there a way to call an external program inside python and dont 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. 解决方案 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()
经验分享 程序员 微信小程序 职场和发展