title: Python socket.gethostbyname_ex() multithread fails
body: <p>I programmed a script that should resolve multiple hostnames into ip addresses using Multithreading.</p>
  
  <p>However, it fails and freezes at some random point. How can this be solved?</p>
  
  <pre><code>num_threads = 100
  conn = pymysql.connect(host='xx.xx.xx.xx', unix_socket='/tmp/mysql.sock', user='user', passwd='pw', db='database')
  cur = conn.cursor()
  def mexec(befehl):
      cur = conn.cursor()
      cur.execute(befehl)
  
  websites = ['facebook.com','facebook.org' ... ... ... ...] \#10.000 websites in array
  queue = Queue()
  def getips(i, q):
      while True:
          #--resolve IP--
          try:
              result = socket.gethostbyname_ex(site)
              print(result)
              mexec("UPDATE sites2block SET ip='"+result+"', updated='yes' ") #puts site in mysqldb
          except (socket.gaierror):
              print("no ip")
              mexec("UPDATE sites2block SET ip='no ip', updated='yes',")
          q.task_done()
  #Spawn thread pool
  for i in range(num_threads):
      worker = Thread(target=getips, args=(i, queue))
      worker.setDaemon(True)
      worker.start()
  #Place work in queue
  for site in websites:
      queue.put(site)
  #Wait until worker threads are done to exit
  queue.join()
  </code></pre>