title: Open named pipe for writing and use it in select()
body: <p>My overall goal is to pass report information into a named pipe if (and only if) there is a reader connected to that named pipe.  If there is no reader, I want to avoid building the report information etc.  Since this is happening in a scenario where other data streams are handled via <code>select()</code>, I want to add the named pipe to the streams which are "ready for writing".</p>
  
  <p>So, I want to open a named pipe for writing and need to give it to <code>select()</code> in a way so that <code>select()</code> only returns in case there really is a reader on the other end of the named pipe.</p>
  
  <p>Normally, if there is no reader, the <code>open</code> call already hangs; unfortunately, I cannot give an <code>open</code> action to <code>select()</code>—only open file descriptors are valid.</p>
  
  <p>To get a decent file descriptor, I'm currently creating a dummy reader (I open the named pipe for reading myself), then open it for writing, and then close the dummy reader again:</p>
  
  <pre><code>dummy = os.open(fifoPath, os.O_RDONLY | os.O_NONBLOCK)
  # ^^^ without the NONBLOCK, this will hang until another process is ready to write
  fifo = os.open(fifoPath os.O_WRONLY)
  os.close(dummy)
  </code></pre>
  
  <p>If I now open another reader, he will receive what I write into the named pipe, so this aspect is working.</p>
  
  <p>But now <code>select()</code> always returns the <code>fifo</code> as ready-to-write, even if there is no reader connected:</p>
  
  <pre><code>r, w, e = select.select([], [ fifo ], [])
  print w[0]
  </code></pre>
  
  <p>This will always and at once print the <code>fifo</code> value.</p>
  
  <p>And writing into the named pipe also does not hang, no matter how much I write:</p>
  
  <pre><code>fifo.write('foo')
  </code></pre>
  
  <p>The data I write just is lost as no reader is connected.</p>
  
  <p>When opening my dummy reader, I am passing the flag <code>O_NONBLOCK</code> because otherwise this opening hangs as well.  I tried various methods but could not figure out how to do this properly.</p>
  
  <p>Can anybody tell me how to combine writing into named pipes with <code>select()</code>?</p>
  
  <p>(The code above is in Python but I guess the problem is not really connected to Python but rather a Linux/Unix problem, so I won't tag this question as Python.)</p>