I came across several flow control examples using the streaming module (using Python 2.6).
I am trying to understand how this example calls the "run" method and where. I can not see anything. The ThreadUrl class receives an instance of the main () function as "t", and it is here that I usually expect the code to start the "run" method.
Maybe this is not the best way to work with threads? Please enlighten me:
import Queue
import time
import urllib2
import threading
import datetime
hosts = ["http://example.com/", "http://www.google.com"]
queue = Queue.Queue()
class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
host = self.queue.get()
url = urllib2.urlopen(host)
print url.read(10)
self.queue.task_done()
start = time.time()
def main():
for i in range(1):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
for host in hosts:
queue.put(host)
queue.join()
main()
print "Elapsed time: %s" % (time.time() - start)
source
share