如何解决在Windows上使用close_fds=True和重定向stdout/stderr的问题

3

我遇到了一个问题:在使用 Python 2.7 的情况下,无法创建子进程。

subprocess.Popen([.......], close_fds=True, stdout=subprocess.PIPE, ...)

在Windows上,由于限制,我需要使用close_fds,因为我不希望子进程继承已经打开的文件描述符。这是在库中调用的,这意味着我无法控制已经打开的文件描述符(N标志)。
这是一个已知的错误,在Python 3.4+中得到了修复。
我的问题是:如何在不出现“close_fds不支持在Windows平台上重定向stdin/stdout/stderr”的情况下使用子进程?
以下是答案
1个回答

5

这个问题在Python 3.7及以上版本中默认已经解决

这绝对是一个棘手的技巧:答案是在使用subprocess模块之前,遍历已经打开的文件描述符。

def _hack_windows_subprocess():
    """HACK: python 2.7 file descriptors.
    This magic hack fixes https://bugs.python.org/issue19575
    by adding HANDLE_FLAG_INHERIT to all already opened file descriptors.
    """
    # See https://github.com/secdev/scapy/issues/1136
    import stat
    from ctypes import windll, wintypes
    from msvcrt import get_osfhandle

    HANDLE_FLAG_INHERIT = 0x00000001

    for fd in range(100):
        try:
            s = os.fstat(fd)
        except:
            continue
        if stat.S_ISREG(s.st_mode):
            handle = wintypes.HANDLE(get_osfhandle(fd))
            mask   = wintypes.DWORD(HANDLE_FLAG_INHERIT)
            flags  = wintypes.DWORD(0)
            windll.kernel32.SetHandleInformation(handle, mask, flags)

这是一段没有它就会崩溃的示例代码:
import os, subprocess
f = open("a.txt", "w")
subprocess.Popen(["cmd"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
f.close()
os.remove(f.name)

追溯(Traceback)(最近的调用在最上面):

文件 "stdin",第 1 行,模块中

WindowsError: [错误 32] 进程无法访问文件,因为该文件正被另一个进程使用:'a.txt'

现在已经修复:

import os, subprocess
f = open("a.txt", "w")
_hack_windows_subprocess()
subprocess.Popen(["cmd"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
f.close()
os.remove(f.name)

操作正常。

希望我有所帮助。


在Windows上的Python 2.7中,您可以明确地将文件打开为不可继承的,例如 f = open("a.txt", "wN")。在Python 3中,默认情况下就是这样的。 - Eryk Sun
这是正确的,上述例子仅在程序无法控制已打开文件时使用 (例如 API)。 - Cukic0d
2
此外,在3.7版(仍处于测试阶段),subprocess使用PROC_THREAD_ATTRIBUTE_HANDLE_LIST来限制哪些可继承的句柄实际上被继承,从而解决了这个问题。 - Eryk Sun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接