[Python-Dev] Forking and pipes

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Dec 10 01:00:40 CET 2008


Lars Kotthoff wrote:

> This prints out "foo" twice although it's only written once to the pipe. It
> seems that python doesn't flush file descriptors before copying them to the
> child process, thus resulting in the duplicate message. The equivalent C
> program behaves as expected,

Your Python and C programs are not equivalent -- the C one is
writing directly to the file descriptor, whereas the Python one
is effectively using a buffered stdio stream. The unflushed stdio
buffer is getting copied by the fork, hence the duplicate output.

Solution: either (a) flush the Python file object before forking
or (b) use os.write() directly on the fd to avoid the buffering.

-- 
Greg


More information about the Python-Dev mailing list