title: Why does a read-only open of a named pipe block?
body: <p>I've noticed a couple of oddities when dealing with named pipes (FIFOs) under various flavors of UNIX (Linux, FreeBSD and MacOS X) using Python.  The first, and perhaps most annoying is that attempts to open an empty/idle FIFO read-only will block (unless I use <code>os.O_NONBLOCK</code> with the lower level <code>os.open()</code> call).  However, if I open it for read/write then I get no blocking.</p>
  <p>Examples:</p>
  <pre><code>f = open('./myfifo', 'r')               # Blocks unless data is already in the pipe
  f = os.open('./myfifo', os.O_RDONLY)    # ditto
  
  # Contrast to:
  f = open('./myfifo', 'w+')                           # does NOT block
  f = os.open('./myfifo', os.O_RDWR)                   # ditto
  f = os.open('./myfifo', os.O_RDONLY|os.O_NONBLOCK)   # ditto
  </code></pre>
  <p><strong>Note:</strong> The behavior is NOT Python specific.  Example in Python to make it easier to replicate and understand for a broader audience).</p>
  <p>I'm just curious why.  Why does the open call block rather than some subsequent read operation?</p>
  <p>Also I've noticed that a non-blocking file descriptor can exhibit two different behaviors in Python.  In the case where I use <code>os.open()</code> with the <code>os.O_NONBLOCK</code> for the initial opening operation, then an <code>os.read()</code> seems to return an empty string if data is not ready on the file descriptor.  However, if I use <code>fcntl.fcnt(f.fileno(), fcntl.F_SETFL, fcntl.GETFL | os.O_NONBLOCK)</code> then an <code>os.read</code> raises an exception (<code>errno.EWOULDBLOCK</code>)</p>
  <p>Is there some other flag being set by the normal <code>open()</code> that's not set by my <code>os.open()</code> example?  How are they different and why?</p>