2

I have the following sample code:

#! /usr/bin/python2.7
import os
import errno
FIFO = 'mypipe'

try:
    os.mkfifo(FIFO)
except OSError as oe: 
    if oe.errno != errno.EEXIST:
        raise
    with open(FIFO) as fifo:
        test=fifo.read()
        print("FIFO opened")
        while True:
            print "reading fifo"
            data = fifo.read()
            print "python read"
            if len(data) == 0:
                print("Writer closed")
                break
        print "about to open pipe for writing"
        otherpipe = open('mypipereader', 'r+')
        otherpipe.write('hello back!')

This works fine. In fact while echo'ing trace into the pipe it does exactly what I want it to BUT for some reason in another script when I try and open the pipe for writing in another program like....

THEPIPE = open('mypipe', 'w') THEPIPE.write("hello!")

it continues to hang! Someone told me once that it has something to do with the Kernel not being able to open the pipes for WRITING before READING... is there any way I can get around this?

Thank you in advance!

driedupsharpie
  • 133
  • 2
  • 10
  • Possible duplicate of [How do I properly write to FIFOs in Python?](http://stackoverflow.com/questions/7048095/how-do-i-properly-write-to-fifos-in-python) – stovfl May 08 '17 at 08:15
  • Could you please provide an [MCVE](http://stackoverflow.com/help/mcve) that demonstrates the problem? Cut out the part that we don't need (e.g., open `mypipereader`), and show actual client demonstration code that doesn't work the way you expect. (You may find that both the client `open` and `write` succeed...) – pilcrow May 08 '17 at 17:31

1 Answers1

4

I was struggling with this and eventually solved my problem by creating a file descriptor first:

import os

fifo = 'my-fifo'
os.mkfifo(fifo)

fd = os.open(fifo, os.O_RDWR) #non-blocking
f = os.fdopen(fd, 'w') #also non-blocking