2015-11-13 62 views
2

读书时,我使用python3.4的os.pipe做IPC bettwen父进程和子进程,通过 ARGS os.execlp通过管道ARGS“坏文件描述符”从管中叉过程中python3.4

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:[ERR没有9]错误的文件描述符

,然后我试着写管道:

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

BrokenPipeError:[错误32]破管

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

“版本3.4中更改:新文件描述符现在是非可继承的” 但我不知道它的含义是什么? 我如何使用管道IPC?

+0

你的代码甚至不显示管道创建。请添加它。此外,请参阅如何[创建一个最小的,可验证的示例](http://stackoverflow.com/help/mcve),以获取有关最大化您将获得的答案的有用性的提示。 – spectras

回答

1

它被认为是一个安全漏洞,允许FD被默认继承,因此Python 3.4的改变。您必须通过调用os.set_inheritable(fd, True)将FD明确标记为可继承。请注意,这个函数在Python 3.4中是新的。

+0

'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,真)'' os.set_inheritable (self.child_pipe_write,True)' 'os.set_inheritable(self.parent_pipe_read,True)' 'os.set_inheritable(self.parent_pipe_write,True)' – minghu6

+0

我甚至尝试了一下,仍然抛出OSError坏文件描述符在我的子进程,但.. – minghu6