Howto:close_fds=True 的解决方法并在 Windows 上重定向 stdout/stderr

Howto: workaround of close_fds=True and redirect stdout/stderr on windows(Howto:close_fds=True 的解决方法并在 Windows 上重定向 stdout/stderr)

本文介绍了Howto:close_fds=True 的解决方法并在 Windows 上重定向 stdout/stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题:使用 Python 2.7,无法使用创建子进程

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

由于限制,在 Windows 上.在我的情况下需要使用 close_fds,因为我不希望子进程从已经打开的文件文件描述符继承.这是在库中调用的,这意味着我无法控制已打开的文件描述符(N 标志).

这是一个已知错误,已在Python 3.4+

我的问题是:如何在不获取的情况下使用子流程

<块引用>

如果重定向 stdin/stdout/stderr,Windows 平台不支持 close_fds

下面回答

解决方案

Python 3.7+ 默认已修复此问题

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

def _hack_windows_subprocess():"""HACK:python 2.7 文件描述符.这个神奇的 hack 修复了 https://bugs.python.org/issue19575通过将 HANDLE_FLAG_INHERIT 添加到所有已打开的文件描述符."""# 见 https://github.com/secdev/scapy/issues/1136进口统计从 ctypes 导入windll,wintypes从 msvcrt 导入 get_osfhandleHANDLE_FLAG_INHERIT = 0x00000001对于范围内的 fd(100):尝试:s = os.fstat(fd)除了:继续如果 stat.S_ISREG(s.st_mode):句柄 = wintypes.HANDLE(get_osfhandle(fd))掩码 = wintypes.DWORD(HANDLE_FLAG_INHERIT)标志 = wintypes.DWORD(0)windll.kernel32.SetHandleInformation(句柄、掩码、标志)

这是一个没有它会崩溃的示例:

导入操作系统,子进程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: [Error 32] Le processus ne peut pas accÚder aufichier car ce fichier est utilisÚ par un autre processus: 'a.txt'

现在修复:

导入操作系统,子进程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)

工作.

希望对我有所帮助

I had a problem: using Python 2.7, it was not possible to create a subprocess using

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

On windows, because of limitations. The use of close_fds was needed in my case, as I did not wanted the subprocess to inherit from already opened files file-descriptor. This was called in a library, which meant I had no control over already opened- file descriptors (N flag).

This is a known bug, which was fixed on Python 3.4+

The question I had was: How do I use subprocess without getting

close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderr

Answer below

解决方案

This issue is fixed on Python 3.7+ by default

This is definatly a tricky hack: answer is to iterate through already opened file descriptors, just before using the subprocess module.

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)

Here is a sample that would crash without it:

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 (most recent call last):

File "stdin", line 1, in module

WindowsError: [Error 32] Le processus ne peut pas accÚder au fichier car ce fichier est utilisÚ par un autre processus: 'a.txt'

Now with the fix:

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)

Works.

Hope i helped

这篇关于Howto:close_fds=True 的解决方法并在 Windows 上重定向 stdout/stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Howto:close_fds=True 的解决方法并在 Windows 上重定向 stdout/stderr

基础教程推荐