title: using multiprocessing to handle socket
body: <p>I have a server with main process acepting socket connections and put them in Queue stack and another process monitoring this stack and applying it to pool processes handling connections. All works fine except for one thing:</p>
  
  <ul>
  <li>last connection allways at stuck until another connection appears, it's look like last connection can't be closed, but why?</li>
  </ul>
  
  <pre class="lang-python prettyprint-override"><code>from multiprocessing import Queue, Process, Pool, Manager
  import datetime
  import socket
  
  def get_date():
      return datetime.datetime.now().strftime('%H:%M:%S')
  
  
  class Server:
      def __init__(self, host, port):
          self.server_address = host, port
          self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  
      def run(self):
          self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
          self.server_socket.bind(self.server_address)
          self.server_socket.listen(1)
  
          print('listen at: %s:%s' % self.server_address)
  
          q = Manager().Queue()
          Process(target=self.handle_request, args=(q,)).start()
  
          while True:
              client_socket, adress = self.server_socket.accept()
              print('\n[%s] request from: %s:%s' % (get_date(), *adress))
              q.put(client_socket)
              client_socket.close()
              del client_socket  # client_socket.close() not working
  
  
      def help(self, client_socket):
          data = client_socket.recv(512)
          client_socket.send(data)
          client_socket.close()
          print(data[:50])
  
      def handle_request(self, q):
          with Pool(processes=2) as pool:
              while True:
                  pool.apply_async(self.help, (q.get(),))
  
  
  Server('localhost', 8000).run()
  </code></pre>