简体   繁体   English

阻止Python子进程在Windows上传递fds?

[英]Prevent Python subprocess from passing fds on Windows?

Python's subprocess module by default passes all open file descriptors to any child processes it spawns. Python的子进程模块默认将所有打开的文件描述符传递给它产生的任何子进程。 This means that if the parent process is listening on a port, and is killed, it cannot restart and begin listening again (even using SO_REUSEADDR) because the child is still in possession of that descriptor. 这意味着,如果父进程正在端口上侦听并被杀死,则它无法重新启动并再次开始侦听(即使使用SO_REUSEADDR),因为子进程仍拥有该描述符。 I have no control over the child process. 我无法控制子进程。

The subprocess POpen constructor does accept a close_fds argument, which would close descriptors on the child, just as I want. 子进程POpen构造函数确实接受close_fds参数,该参数将关闭子级上的描述符,就像我想要的那样。 However, there is a restriction, only on Windows, that prevents it from being used if stdin/stdout are also overridden, which I need to do. 但是,只有在Windows上有一个限制,如果stdin / stdout也被覆盖,则无法使用它,这是我需要做的。

Does anyone know of a work-around for this on Windows? 有人知道Windows上的解决方法吗?

What seems to be the most relevant information that I can find: SetHandleInformation , referenced in this article, should give you pointers. 似乎是什么,我可以找到最相关的信息: SetHandleInformation ,中引用文章,应该给你指点。

You'll probably need to use pywin32 and/or ctypes to accomplish what you want. 您可能需要使用pywin32和/或ctypes完成所需的操作。

I don't have a windows box around, so this is untested, but I'd be tempted to try the os.dup and os.dup2 methods; 我周围没有窗户,所以未经测试,但是我很想尝试os.dup和os.dup2方法; duplicate the file descriptors and use those instead of the parent ones. 复制文件描述符,并使用其代替父描述符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM