"错误的文件描述符"在python3.4中从fork进程读取管道时

时间:2015-11-13 15:35:31

标签: python linux pipe

我使用python3.4的os.pipe来做父亲进程和子进程之间的IPC, 通过os.execlp args传递管道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)

throw Error OSError:[Errno 9]错误的文件描述符

然后我尝试写入管道:

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

BrokenPipeError:[Errno 32]管道破损

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

"版本3.4中更改:新文件描述符现在是不可继承的" 但我不知道它的含义是什么? 我怎样才能用管道进行IPC?

1 个答案:

答案 0 :(得分:1)

默认情况下,FD被认为是允许继承FD的安全漏洞,因此Python 3.4也发生了变化。您必须通过调用os.set_inheritable(fd, True)将FD明确标记为可继承。请注意,此函数是Python 3.4中的新功能。

相关问题