从Python启动一个完全独立的过程 [英] Launch a totally independent process from Python

查看:178
本文介绍了从Python启动一个完全独立的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从python启动一个完全独立的进程.我不能使用像os.startfile这样的简单内容,因为我需要传递参数.目前,我正在使用subprocess.popen,它可以让我90%地到达那里.

I'm trying to launch a completely independent process from python. I can't use something simple like os.startfile since I need to pass arguments. Currently I'm using subprocess.popen which gets me 90% of the way there.

args = ["some_exe.exe", "some_arg", "another_arg"]
subprocess.Popen(args, creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

将popen与分离的创建标记一起使用& std *的管道确实启动了一个新进程,该进程在父进程死亡之后仍然存在.这样就很好.问题在于,新的子级"进程仍然为父级保留一个幻像句柄.因此,如果我尝试卸载父exe(我的python脚本通过pyinstaller捆绑到exe中),则msiexec抱怨父exe仍在使用中.

Using popen with detached creation flags & pipes for std* does start a new process that lives after the parent process dies. So thats all good. The problem is that the new 'child' process still holds a phantom handle to the parent. So if I try to uninstall the parent exe (my python script is bundled into an exe via pyinstaller) msiexec complains that the parent exe is still in use.

因此,目标是产生一个完全独立的进程来运行"some_exe.exe",而该进程没有返回原始进程的任何句柄.

So the goal is to spawn a totally independent process to run "some_exe.exe" that doesn't have any handle back to the original process.

注意:这适用于Windows XP及更高版本.我正在Win7上进行开发.

Note: This is for Windows XP and above. I'm developing on Win7.

推荐答案

我想我找到了答案.通过将Popenclose_fds = True结合使用,我可以启动一个独立且没有父级句柄的进程.

I think I found the answer. By using Popen with close_fds = True I was able to start up a process that was independent and without handles to the parent.

对于文档,请此处,然后搜索close_fds.

For docs look here and search for close_fds.

或者,在Windows上,如果close_fds为true,则不会继承任何句柄 通过子进程.请注意,在Windows上,您无法设置close_fds 设置为true并通过设置stdin重定向标准句柄, 标准输出或标准错误.

Or, on Windows, if close_fds is true then no handles will be inherited by the child process. Note that on Windows, you cannot set close_fds to true and also redirect the standard handles by setting stdin, stdout or stderr.

请注意,此解决方案仅适用于Windows.我不知道任何* nix系统.

Note this solution only works on Windows. I have no idea about any *nix system.

这篇关于从Python启动一个完全独立的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆