如何从 python 调用程序而不等待它返回

2022-01-18 00:00:00 python subprocess popen command

问题描述

有没有办法从 python 调用程序而不等待它返回?我创建了一个脚本,它将程序复制到目录并运行该程序.但是当我从 python 调用程序时,python 脚本在我启动的程序退出之前不会退出.我试过 os.system 和 Popen.还有其他方法吗?

is there a way to call a program from python without waiting for it to return? i created a script which copies a program to a directory and runs that program. but when i call the program from python, the python script does not exit until the program i launched exits. i have tried os.system and Popen. is there another way to do this?

添加信息:os.spawnl 和 os.P_DETACH 仍然不起作用;根据文档,P_DETACH 类似于 P_NOWAIT,但新进程与调用进程的控制台分离".但它仍然以某种方式附加到我的调用进程(调用脚本在任何被调用的可执行文件返回之前不会退出)

Added info: os.spawnl with os.P_DETACH still doesn't work; according to the docs, "P_DETACH is similar to P_NOWAIT, but the new process is detached from the console of the calling process". but it is still somehow attached to my calling process (calling script won't quit until any of the called executables return)

程序:

os.system("start test.exe")
print "Done"

在它执行 test.exe 之后,它会打印 Done.但它不会终止脚本的执行(脚本进程仍在运行).尝试创建一个守护线程并使用 P_DETACH Popen,仍然不行.

after it executes test.exe, it prints Done. but it does not terminate the script's execution (script process still running). tried creating a daemon thread and Popen with a P_DETACH, still no go.


解决方案

在 Windows 下,如果你使用 shell START 命令调用程序,你应该能够释放"父进程并允许它退出.在 DOS 提示符下尝试 START/? 以了解更多信息.

Under Windows, if you invoke the program using the shell START command you should be able to "release" the parent process and allow it to exit. Try START /? at the DOS prompt to learn more.

相关文章