微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python:Howto启动一个完整的进程而不是subprocess并检索PID

我想要:

从我的进程(myexe.exe arg0)启动一个新进程(myexe.exe arg1)

检索这个新进程的PID(os窗口)

当我用TaskManager的Windows命令“结束进程树”杀死我的第一个实体(myexe.exe arg0)时,我需要新的(myexe.exe arg1)不会被杀死…

我玩过subprocess.Popen,os.exec,os.spawn,os.system …没有成功。

解决问题的另一种方法:如果有人杀死myexe.exe(arg0)的“进程树”,如何保护myexe.exe(arg1)?

编辑:同样的问题(没有答案)在这里

安装nokogiri时出错

调整窗口大小调整Powershell屏幕缓冲区

恼人的空白控制台在我的GUIforms的背面

GetmodulefileNameA返回奇怪的结果

事件查看器中的行号

编辑:下面的命令不保证subprocess的独立性

subprocess.Popen(["myexe.exe",arg[1]],creationflags = DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,close_fds = True)

两个程序之间的input输出通信

在Windows中与mkstemp(char * template)类似的函数

在Win7 Aero主题增加DateTimePicker日历的字体大小

复制巨大(120 + GB)文件时速度变慢

有没有一种好的方法来绕过运行EXE文件的限制?

要启动父进程在Windows上退出后可以继续运行的子进程,请执行以下操作:

from subprocess import Popen,PIPE CREATE_NEW_PROCESS_GROUP = 0x00000200 DETACHED_PROCESS = 0x00000008 p = Popen(["myexe.exe","arg1"],stdin=PIPE,stdout=PIPE,stderr=PIPE,creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) print(p.pid)

Windows进程创建标志在这里

更便携的版本在这里

几年前我在Windows上做过类似的事情,我的问题是想要杀死子进程。

我认为你可以使用pid = Popen(["/bin/mycmd","myarg"]).pid来运行子pid = Popen(["/bin/mycmd","myarg"]).pid所以我不确定真正的问题是什么,所以我猜测这是当你杀死主进程。

IIRC这是与国旗有关的事情。

我不能证明这一点,因为我没有运行Windows。

subprocess.CREATE_NEW_CONSOLE The new process has a new console,instead of inheriting its parent's console (the default). This flag is always set when Popen is created with shell=True. subprocess.CREATE_NEW_PROCESS_GROUP A Popen creationflags parameter to specify that a new process group will be created. This flag is necessary for using os.kill() on the subprocess. This flag is ignored if CREATE_NEW_CONSOLE is specified.

所以,如果我理解你的代码应该是这样的:

from subprocess import Popen,PIPE script = "C:myexe.exe" param = "-help" DETACHED_PROCESS = 0x00000008 CREATE_NEW_PROCESS_GROUP = 0x00000200 pid = Popen([script,param],shell=True,creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)

至少我试过这个,为我工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐