Python open a named pipe == hanging?

Alex Martelli aleax at mac.com
Mon Aug 7 21:47:00 EDT 2006


Donn Cave <donn at u.washington.edu> wrote:
   ...
> > > I believe your problem is that, by the time you open the
> > > pipe for read, it has already been closed by its writer.
> > 
> > Hmmm, no: the problem is, he never opens the pipe for write, because the
> > open blocks (will not proceed until somebody opens the fifo for reading,
> > which in turn won't happen here because the open blocks).
> > 
> > Try:
> > 
> > a = open('my_fifo', 'w')
> > b = os.popen('cat my_fifo')
> > a.write ...
> > a.close()
> > c = b.read()
> > 
> > this STILL doesn't work, since the very first statement blocks.  (I've
> > also removed the 'r+' mode in favor of 'w', since opening a FIFO for
> > reading AND writing produces undefined behavior, at least in all Unix
> > versions and variants I'm familiar with).
> 
> But it does work.  I edited that excerpt only to complete
> missing parts, and ran it on MacOS X and GNU Linux.
> 
> import os
> f = '/tmp/r'
> try:
>         os.unlink(f)
> except:
>         pass

You forgot to add os.mkfifo(f) here -- so you're writing and reading a
perfectly ordinary file... of course *that* gives no problems!-)


Alex




More information about the Python-list mailing list