Advertisement
peter9477

Parallel request handler for Python 3.x

Jan 20th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1.  
  2. import threading
  3. import queue
  4.  
  5.  
  6. class MultiRequest:
  7.     DEFAULT_NUM_THREADS = 4
  8.  
  9.     def __init__(self, handler, num_threads=DEFAULT_NUM_THREADS):
  10.         self.num_threads = num_threads
  11.         self.threads = []
  12.         self.queue = queue.Queue()
  13.         self.handler = handler
  14.  
  15.  
  16.     def get(self, requests):
  17.         if not self.threads:
  18.             t = threading.Thread(target=self.run)
  19.             t.daemon = True
  20.             t.start()
  21.             self.threads.append(t)
  22.  
  23.         for r in requests:
  24.             self.queue.put(r)
  25.  
  26.  
  27.     def run(self):
  28.         '''called in a thread to process requests by calling the handler'''
  29.         while 1:
  30.             url = self.queue.get()
  31.             self.handler(url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement