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.
In addition to requiring the
'w'
option to the sending module, you may also be having issues with either the sending or receiver not being connected. In order for another process to make use of a fifo, both the sender and receiver must have the fifo handle open.See the linux documentation on a fifo. If the receiver is not listening, you get a SIGPIPE which typically will terminate a process but in python's case it will wait until a receiver is connected.
If your sender is dead and the listener is still active it received an EOF and stops reading.