Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

os.pipe() + os.fork()

3 views
Skip to first unread message

Sebastian Noack

unread,
Feb 20, 2010, 5:36:04 AM2/20/10
to pytho...@python.org
Hi,

I have a problem using os.pipe() together with os.fork(). Usually when
the writing end of the pipe is closed, the reading end gets EOF. So
subsequent attempts to read data will return an empty string. But when
you call os.fork() after you have created a pipe using os.pipe(), and
read data from the pipe in the child process, when the partent process
has already closed the writing end, the reading end does not get EOF.
Instead of the os.read() call in the child process is blocking.

I am using Linux, so I would like to know if this is a bug in Python or
just weird behaviour under Linux and if it is possible work around it.

Regards
Sebastian Noack


---------------------------------------------------------------------------

import os

def test_pipe_sync():
pipe = os.pipe()

os.write(pipe[1], 'Spam')
os.close(pipe[1])

print repr(os.read(pipe[0], 4)) # 'Spam' is printed.
print repr(os.read(pipe[0], 4)) # '' is printed, because of EOF is reached.

def test_pipe_forked():
pipe = os.pipe()
pid = os.fork()

if pid:
os.write(pipe[1], 'Spam')
os.close(pipe[1])

os.waitpid(pid, 0)
else:
print repr(os.read(pipe[0], 4)) # 'Spam' is printed.
print repr(os.read(pipe[0], 4)) # Nothing is printed and os.read()
# is blocking, eventhough the other
# end of the pipe was closed.

if __name__ == '__main__':
test_pipe_sync()

print
print '-' * 80
print

test_pipe_forked()

signature.asc

Sebastian Noack

unread,
Feb 20, 2010, 6:52:50 AM2/20/10
to pytho...@python.org
I have figured out that, you have to close the writing end in the child
process, which is reading from the pipe. Otherwise the underlying pipe
is not going to be closed when the parent process is closing its
writing end. This has nothing to do with Python itself. I have tried
plain C and there it is the same behaviour.

Regards
Sebastian Noack

signature.asc

Gary Herron

unread,
Feb 20, 2010, 2:13:45 PM2/20/10
to pytho...@python.org


Correct. The fork creates two processes with references to the read and
write ends of the pipe. Both parent and child processes should close
the ends they are not using.

Here's a thought: Consider the subprocess module. It can do the fork
and any necessary pipes and can do so in an OS independent way. It
might make you life much easier.

Gary Herron

sebasti...@googlemail.com

unread,
Feb 23, 2010, 4:24:48 AM2/23/10
to
On Feb 20, 8:13 pm, Gary Herron <gher...@islandtraining.com> wrote:
> Here's a thought:  Consider the subprocess module.   It can do thefork
> and any necessary pipes and can do so in an OS independent way.   It
> might make you life much easier.

As far as i know the subprocess module provides only functionality for
running any program as subprocess. But I just want to fork the current
process without putting some code in an external python script.

Sebastian Noack

Aahz

unread,
Feb 25, 2010, 3:23:30 PM2/25/10
to
In article <256926de-e175-4fa2...@u20g2000yqu.googlegroups.com>,
sebasti...@googlemail.com <sebasti...@googlemail.com> wrote:
>On Feb 20, 8:13=A0pm, Gary Herron <gher...@islandtraining.com> wrote:
>>
>> Here's a thought: =A0Consider the subprocess module. =A0 It can do thefor=
>k
>> and any necessary pipes and can do so in an OS independent way. =A0 It

>> might make you life much easier.
>
>As far as i know the subprocess module provides only functionality for
>running any program as subprocess. But I just want to fork the current
>process without putting some code in an external python script.

Then try multiprocessing
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer

0 new messages