Opening named pipe in one module, reading in the other

Viewed 3077

I'm hot on the trail of figuring something out for one of my projects, but hung up on one issue:

I'm using a FIFO operation to send a 'signal' (simple T/F) from one module to another. One module opens the FIFO to write to it, and the other opens the FIFO to read from it. The goal here is to have the reading module immediately read and display as soon as the writing module receives the command to do so. The writing module opens the FIFO, but the reading module doesn't seem to do so.

Is what I'm trying to do even possible? I'm trying to spin both operations in _threads in order to keep multiple processes going in each module. Note both modules are in classes that I didn't include for the sake of brevity (explaining 'self').

original sending module

def pipe_relay(self):
    FIFO = 'pipe_relay'
    thread_num = num

    try:
        os.mkfifo(FIFO)
    except OSError as oe:
        if oe.errno != errno.EEXIST:
            raise

    while self.relay_switch:
        print("Opening FIFO...")
        with open(FIFO) as fifo:
            print("FIFO opened")
            while self.relay_switch:
                data = fifo.write(signal)
                if len(data) == 0:
                    print("Writer is closed")
                    break
                print('Write: "{0}"'.format(data))

updated sending module

I realized that I didn't want to be writing to the FIFO continuously with the data I threw at it, so I removed the while() statement. Now, it doesn't seem as though the FIFO will open at all...


def pipe_relay(self, num, signal):
    FIFO = 'pipe_relay'
    thread_num = num

    try:
        os.mkfifo(FIFO)
    except OSError as oe:
        if oe.errno != errno.EEXIST:
            raise

    print("Opening FIFO...")

    # does not proceed past this point

    with open(FIFO, mode = 'w') as fifo:
        print("FIFO opened")
        data = fifo.write(signal)
        if len(data) == 0:
            print("Writer is closed")
        print('Write: "{0}"'.format(data))
        fifo.close()

receiving module

def pipe_receive(self):
    FIFO = 'pipe_relay'

    try:
        os.mkfifo(FIFO)
    except OSError as oe:
        if oe.errno != errno.EEXIST:
            raise   

    # module proceeds to here, but no further

    with open(FIFO) as fifo:
        print("FIFO opened (receiver)")
        while True:
            data = fifo.read()
            if len(data) == 0:
                print("Writer is closed")
                break
            print('Read signal: "{0}"'.format(data))
            self.DISPLAY['text'] = data
    print("this is in 'pipe_receieve'")

EDIT

Running Ubuntu 17.04. The project is written for the Python 3.5 interpreter.

3 Answers
Related