Python open a named pipe == hanging?

Rochester rochester1976 at gmail.com
Thu Aug 3 16:22:08 EDT 2006


Hi,

     I just found out that the general open file mechanism doesn't work
     for named pipes (fifo).  Say I wrote something like this and it
     simply hangs python:

#!/usr/bin/python

import os

os.mkfifo('my_fifo')

open('my_fifo', 'r+').write('some strings.')
x = os.popen('cat my_fifo').read()

print x


     I know I could use a tempfile instead of a fifo in this very
     simple case, I just want to know is there a standard way of
     handling fifos withing python.  Especially the non-trivial case
     when I want to call a diff like system program which takes two
     files as input.  Say I have two python string objects A and B, I
     want to call diff to see what is the different between those two
     strings, and return the finding as a string obj C back to python.
     This certainly can be done in this way:

open('tmpfile1', 'w').write(A)
open('tmpfile2', 'w').write(B)
C = os.popen('diff tmpfile1 tmpfile2').read()

     But that's kinda awkward isn't it? :-) The Bash way of doing this
     would be (suppose A is the stdout of prog2, B is the stdout of
     prog3):

diff <(prog2) <(prog3) > C

What is the best way of doing this in Python?

Thank you!



More information about the Python-list mailing list