从管道读取时Python“错误文件描述符”

问题描述 投票:0回答:1

我正在尝试启动一个子进程并将管道文件描述符传递给它以供读取。然而,当我尝试从子进程中的管道中读取时,我得到“错误文件描述符”,即使我可以从父进程中的管道中读取得很好。

这是父进程:

import subprocess
import sys
import os

r, w = os.pipe()

os.set_inheritable(r, True)

p = subprocess.Popen(["python3", "client.py", str(r)])

os.write(w, b"hello")

p.wait()

这是子进程:

import sys
import os

r = int(sys.argv[1])
print("[Client]", os.read(r,  5))

任何帮助将不胜感激。

python python-3.x pipe
1个回答
4
投票

来自Python官方docs

使用 subprocess 模块,除了标准流之外的所有文件描述符都被关闭,并且只有当 close_fds 参数为

False
时才继承可继承句柄。

© www.soinside.com 2019 - 2024. All rights reserved.