title: Too many open files. Python multiprocess TCP server
body: <p>I am writing tcp server on Python. Os - ubuntu 12.04
  The server is multiprocess. Main process is accepting connections. Accepted socket reduced and sent to the worker:</p>
  
  <pre><code>&lt;main process&gt;
  h = reduce_handle(conn.fileno())
  self.queue.put(h)
  </code></pre>
  
  <p>Worker creates separated Thread for this connection:</p>
  
  <pre><code>&lt;worker process&gt;
  t = threading.Thread(target=sock_thread, args=(h, DBSession, Protocol))
  t.start()
  </code></pre>
  
  <p>Reduced socket is recovered and working at the separeted Thread:</p>
  
  <pre><code>&lt;Connection Thread&gt;
  fd=rebuild_handle(h)
  sock = socket.fromfd(fd,socket.AF_INET,socket.SOCK_STREAM)
  
  &lt;data transmition&gt;
  
  sock.close()
  </code></pre>
  
  <p>Everything works fine. Today I has got an exception:</p>
  
  <pre><code>error: [Errno 24] Too many open files
  </code></pre>
  
  <p>Restarting server solved this problem, but number of unclosed files are increased. I monitor it trow command line:</p>
  
  <pre><code>lsof | grep python | wc -l 
  </code></pre>
  
  <p>What is the problem? I close each socket at the thread. All threads are normally works and finishes. sock.shutdown(socket.SHUT_RDWR) before socket closing raises exception: bad file descriptor.
  Is there a way to close the file associated with the socket?</p>
  
  <p>Thanks.</p>