I have a Python3 process on my Unix system that always works, and I want to be able to randomly send data through a named pipe from other processes that run only occasionally. If the named pipe has no data, I want my process to continue to do other things, so I need to check if it has data without blocking .
I cannot figure out how to check without opening it, but opening blocks, if I did not set the flag without blocking. And if I set the flag, it will work if I write to the pipe before or during reading.
This is the best I have managed to do:
import os
fifo = "pipe_test.fifo"
done = False
fd = os.open(fifo, os.O_RDONLY | os.O_NONBLOCK)
while not done:
try:
s = os.read(fd, 1024)
print(s)
done = True
except BlockingIOError as e:
pass
os.close(fd)
, b"", . , , , . , - . ?