以下命令不保证子进程的独立性subprocess.Popen(["myexe.exe",arg[1]],creationflags = DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,close_fds = True) 解决方案 在 Windows 上启动一个可以在父进程退出后继续运行的子进程:from subprocess import Popen, PIPECREATE_NEW_PROCESS_GROUP = 0x00000200DETACHED_PROCESS = 0x00000008p = Popen(["myexe.exe", "arg1"], stdin=PIPE, stdout=PIPE, stderr=PIPE,creationflags=DETACHED_PROCESS |CREATE_NEW_PROCESS_GROUP)打印(p.pid)Windows 进程创建标志是 这里更便携的版本在这里.I would like:Launch a new process (myexe.exe arg1) from my process (myexe.exe arg0)Retrieve the PID of this new process (os windows)when I kill my first entity (myexe.exe arg0) with the TaskManager Windows Command "End process tree", I need that the new one (myexe.exe arg1) will not be killed...I've played with subprocess.Popen, os.exec, os.spawn, os.system... without success.Another way to explain the problem: How to protect myexe.exe (arg1) if someone kills the "process tree" of the myexe.exe (arg0)?EDIT: same question (without answer) HEREEDIT: the following command do not guarantee the Independence of the subprocesssubprocess.Popen(["myexe.exe",arg[1]],creationflags = DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,close_fds = True) 解决方案 To start a child process that can continue to run after the parent process exits on Windows:from subprocess import Popen, PIPECREATE_NEW_PROCESS_GROUP = 0x00000200DETACHED_PROCESS = 0x00000008p = Popen(["myexe.exe", "arg1"], stdin=PIPE, stdout=PIPE, stderr=PIPE, creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)print(p.pid)Windows process creation flags are hereA more portable version is here. 这篇关于Python:如何启动一个完整的进程而不是子进程并检索 PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 05:16