title: Need some assistance with Python threading/queue
body: <pre><code>import threading
  import Queue
  import urllib2
  import time
  
  class ThreadURL(threading.Thread):
  
      def __init__(self, queue):
          threading.Thread.__init__(self)
  
          self.queue = queue
  
      def run(self):
          while True:
              host = self.queue.get()
              sock = urllib2.urlopen(host)
              data = sock.read()
  
              self.queue.task_done()
  
  hosts = ['http://www.google.com', 'http://www.yahoo.com', 'http://www.facebook.com', 'http://stackoverflow.com']
  start = time.time()
  
  def main():
      queue = Queue.Queue()
  
      for i in range(len(hosts)):
          t = ThreadURL(queue)
          t.start()
  
      for host in hosts:
          queue.put(host)
  
      queue.join()
  
  if __name__ == '__main__':
      main()
      print 'Elapsed time: {0}'.format(time.time() - start)
  </code></pre>
  
  <p>I've been trying to get my head around how to perform Threading and after a few tutorials, I've come up with the above.</p>
  
  <p>What it's supposed to do is:</p>
  
  <ol>
  <li>Initialiase the queue</li>
  <li>Create my Thread pool and then queue up the list of hosts</li>
  <li>My ThreadURL class should then begin work once a host is in the queue and read the website data</li>
  <li>The program should finish</li>
  </ol>
  
  <p>What I want to know first off is, am I doing this correctly? Is this the best way to handle threads?</p>
  
  <p>Secondly, my program fails to exit. It prints out the <code>Elapsed time</code> line and then hangs there. I have to kill my terminal for it to go away. I'm assuming this is due to my incorrect use of <code>queue.join()</code> ?</p>