Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. class ThreadUrl(threading.Thread):
  2. """Threaded Url Grab"""
  3. def __init__(self, queue, out_queue):
  4. threading.Thread.__init__(self)
  5. self.queue = queue
  6. self.out_queue = out_queue
  7.  
  8. def run(self):
  9. while True:
  10. #grabs host from queue
  11. host = self.queue.get()
  12.  
  13. #grabs urls of hosts and then grabs chunk of webpage
  14. url = urllib2.urlopen(host)
  15. chunk = url.read()
  16.  
  17. #place chunk into out queue
  18. self.out_queue.put(chunk)
  19.  
  20. #signals to queue job is done
  21. self.queue.task_done()
  22.  
  23. def main():
  24.  
  25. #spawn a pool of threads, and pass them queue instance
  26. for i in range(5):
  27. t = ThreadUrl(queue)
  28. t.setDaemon(True)
  29. t.start()
  30.  
  31. queue.join()
  32.  
  33. class ThreadUrl(threading.Thread):
  34. """Threaded Url Grab"""
  35. def __init__(self, queue, out_queue):
  36. threading.Thread.__init__(self)
  37. self.queue = queue
  38. self.out_queue = out_queue
  39. self.continue_loop = True
  40.  
  41. def run(self):
  42. while self.continue_loop:
  43. #grabs host from queue
  44. host = self.queue.get()
  45.  
  46. #grabs urls of hosts and then grabs chunk of webpage
  47. url = urllib2.urlopen(host)
  48. chunk = url.read()
  49.  
  50. #place chunk into out queue
  51. self.out_queue.put(chunk)
  52.  
  53. #signals to queue job is done
  54. self.queue.task_done()
  55.  
  56. def main():
  57.  
  58. #spawn a pool of threads, and pass them queue instance
  59. threads = []
  60. for i in range(5):
  61. t = ThreadUrl(queue, out_queue)
  62. t.setDaemon(True)
  63. t.start()
  64. threads.append(t)
  65.  
  66. for t in threads:
  67. t.continue_loop = False
  68. t.join()
  69.  
  70. queue.join()
  71.  
  72. def run(self):
  73. while True:
  74. #grabs host from queue
  75. try:
  76. host = self.queue.get(block=False)
  77. except Queue.Empty, ex:
  78. break
  79. #grabs urls of hosts and then grabs chunk of webpage
  80. url = urllib2.urlopen(host)
  81. chunk = url.read()
  82.  
  83. #place chunk into out queue
  84. self.out_queue.put(chunk)
  85.  
  86. #signals to queue job is done
  87. self.queue.task_done()
  88.  
  89. host = self.queue.get()
  90. if host == 'STOP':
  91. #Still need to signal that the task is done, else your queue join() will wait forever
  92. self.queue.task_done()
  93. break
Add Comment
Please, Sign In to add comment