Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > os.pipe() + os.fork()

Reply
Thread Tools

os.pipe() + os.fork()

 
 
Sebastian Noack
Guest
Posts: n/a
 
      02-20-2010
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()

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkt/uxQACgkQhef27Il4mhh9EACeJ//Khqb0fnjh3z5Qq6iXaMVY
eEsAoOupjftyGBAnn7DSGlHZhOukVrGl
=F59I
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments