What conditions result in an opened, nonblocking named pipe (fifo) being “unavailable” for reads?

前端 未结 1 1709
你的背包
你的背包 2020-12-15 09:05

Situation:

new_pipe = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK) # pipe_path points to a FIFO
data = os.read(new_pipe, 1024)

The read

相关标签:
1条回答
  • 2020-12-15 09:45

    From the POSIX specification of the read system call (emphasis mine):

    When attempting to read from an empty pipe or FIFO:

    • If no process has the pipe open for writing, read() shall return 0 to indicate end-of-file.

    • If some process has the pipe open for writing and O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].

    So basically your second assumption is wrong:

    If the writer has the pipe opened, but no data is in the fifo, empty str ('') is also returned

    This would be against the specification and I can't reproduce that behaviour on my machine (it raises EAGAIN for me). This is not a big problem however, you can just catch the exception and retry:

    import errno
    
    def safe_read(fd, size=1024):
       ''' reads data from a pipe and returns `None` on EAGAIN '''
       try:
          return os.read(fd, size)
       except OSError, exc:
          if exc.errno == errno.EAGAIN:
             return None
          raise
    
    0 讨论(0)
提交回复
热议问题