Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ThreadUrl(threading.Thread):
- """Threaded Url Grab"""
- def __init__(self, queue, out_queue):
- threading.Thread.__init__(self)
- self.queue = queue
- self.out_queue = out_queue
- def run(self):
- while True:
- #grabs host from queue
- host = self.queue.get()
- #grabs urls of hosts and then grabs chunk of webpage
- url = urllib2.urlopen(host)
- chunk = url.read()
- #place chunk into out queue
- self.out_queue.put(chunk)
- #signals to queue job is done
- self.queue.task_done()
- def main():
- #spawn a pool of threads, and pass them queue instance
- for i in range(5):
- t = ThreadUrl(queue)
- t.setDaemon(True)
- t.start()
- queue.join()
- class ThreadUrl(threading.Thread):
- """Threaded Url Grab"""
- def __init__(self, queue, out_queue):
- threading.Thread.__init__(self)
- self.queue = queue
- self.out_queue = out_queue
- self.continue_loop = True
- def run(self):
- while self.continue_loop:
- #grabs host from queue
- host = self.queue.get()
- #grabs urls of hosts and then grabs chunk of webpage
- url = urllib2.urlopen(host)
- chunk = url.read()
- #place chunk into out queue
- self.out_queue.put(chunk)
- #signals to queue job is done
- self.queue.task_done()
- def main():
- #spawn a pool of threads, and pass them queue instance
- threads = []
- for i in range(5):
- t = ThreadUrl(queue, out_queue)
- t.setDaemon(True)
- t.start()
- threads.append(t)
- for t in threads:
- t.continue_loop = False
- t.join()
- queue.join()
- def run(self):
- while True:
- #grabs host from queue
- try:
- host = self.queue.get(block=False)
- except Queue.Empty, ex:
- break
- #grabs urls of hosts and then grabs chunk of webpage
- url = urllib2.urlopen(host)
- chunk = url.read()
- #place chunk into out queue
- self.out_queue.put(chunk)
- #signals to queue job is done
- self.queue.task_done()
- host = self.queue.get()
- if host == 'STOP':
- #Still need to signal that the task is done, else your queue join() will wait forever
- self.queue.task_done()
- break
Add Comment
Please, Sign In to add comment