为什么 `script.py <(cat *.gz)` 在 python 2 中使用 subprocess.Popen 而不是 python 3?

Why does `script.py <(cat *.gz)` work with subprocess.Popen in python 2 but not python 3?

我们最近发现,如果通过进程替换提供其输入文件,我们开发的脚本会在 python 3.x(但不会 python 2.x)中阻塞,例如:

script.py <(cat *.gz)

我们已经使用 gzip 以外的命令(例如 cat)进行了测试,只是为了看看是否会出现类似的错误。他们都抱怨/dev/fd/63(或/dev/fd/63.gz)不存在。这是(简化的)相关代码:

def open_gzip_in(infile):
    '''Opens a gzip file for reading, using external gzip if available'''

    # Determine whether to use the gzip command line tool or not
    if exeExists('gzip'):
        cmd = ['gzip', '-dc', infile]
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=-1,
                             universal_newlines=True)
        if sys.version.startswith("2"):
            with p.stdout:
                for line in iter(p.stdout.readline, b''):
                    yield line
        else:
            with p:
                for line in p.stdout:
                    yield line
        exit_code = p.wait()
        if exit_code != 0:
            raise subprocess.CalledProcessError(
                p.returncode, subprocess.list2cmdline(cmd), 'Ungzip failed')
    else:
        with io.TextIOWrapper(io.BufferedReader(gzip.open(infile))) as f:
            for line in f:
                yield(line)

顺便说一下,我们做 fork 只是因为命令行 gzip 比使用 gzip.open 快得多,而且我们的脚本是一个长 运行 worker - 差异是多方面的小时。

我们正在针对此问题实施解决方法,但想了解为什么它在 python 3 中不起作用但在 python 2 中起作用。

这是新的默认 Popen() 系列参数 close_fds=True 的副作用。您可以使用 close_fds=False 显式覆盖它,您继承的文件描述符将传递给子进程(通过 os.set_inheritable() 进行配置)。

类似地,在 Python 3.2 及更高版本中,您可以使用 pass_fds 列表,如 pass_fds=[0,1,2,63] 中,使 stdin、stdout、stderr 和 FD #63 可用到调用的子流程。