来自man 7 fifo:
A process can open a FIFO in nonblocking mode. In this case, opening or read-only will succeed even if no-one has opened on the write side yet, opening for write-only will fail with ENXIO (no such device or address) unless the other end has already been opened.
所以第一个解决方案是使用O_NONBLOCK打开FIFO.在这种情况下,您可以检查errno:如果它等于ENXIO,那么您可以稍后尝试打开FIFO.
import errno
import posix
try:
posix.open('fifo', posix.O_WRONLY | posix.O_NONBLOCK)
except OSError as ex:
if ex.errno == errno.ENXIO:
pass # try later
另一种可能的方法是使用O_RDWR标志打开FIFO.在这种情况下它不会阻止.其他进程可以使用O_RDONLY打开它而没有问题.
import posix
posix.open('fifo', posix.O_RDWR)