【问题标题】:"Bad file descriptor" when reading from pipe in the fork process in python3.4从python3.4的fork进程中的管道读取时出现“错误的文件描述符”
【发布时间】:2015-11-13 15:35:31
【问题描述】:

我使用python3.4的os.pipe做IPC betwen的父进程和子进程, 通过 os.execlp args 传递管道参数

  self.child_pipe_read=int(sys.argv[2])
  self.child_pipe_write=int(sys.argv[3])

...

  os.execlp('python3','python3','child_test.py',str(pid),str(self.child_pipe_read) ,str(self.child_pipe_write))

但是,当我使用这个时:

msg=os.read(self.child_pipe_read,32)

抛出错误 OSError: [Errno 9] 错误的文件描述符

然后我尝试写入管道:

 os.write(self.parent_pipe_write,(msg+'\n').encode())

BrokenPipeError: [Errno 32] 损坏的管道

我看到了python3.4的文档,找到这个:

“3.4 版更改:新文件描述符现在不可继承” 但我不知道这是什么意思? 如何使用管道进行 IPC?

【问题讨论】:

  • 您的代码甚至没有显示管道创建。请添加它。另外,请参阅如何create a minimal, verifiable example 以获取有关最大化您将获得的答案的有用性的提示。

标签: python linux pipe


【解决方案1】:

默认情况下允许继承 FD 被认为是一个安全漏洞,因此对 Python 3.4 进行了更改。您必须通过调用 os.set_inheritable(fd, True) 将 FD 显式标记为可继承。请注意,此函数是 Python 3.4 中的新函数。

【讨论】:

  • self.child_pipe_read,self.parent_pipe_write=os.pipe()self.parent_pipe_read,self.child_pipe_write=os.pipe()os.set_inheritable(self.child_pipe_read, True)os.set_inheritable(self.child_pipe_write, True)os.set_inheritable(self.parent_pipe_read, True)os.set_inheritable(self.parent_pipe_write, True)
  • 我什至尝试过打击,但是仍然在我的子进程中抛出 OSError Bad file descriptor ..
猜你喜欢
  • 2010-11-06
  • 1970-01-01
  • 2014-12-07
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多