Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- import queue
- class MultiRequest:
- DEFAULT_NUM_THREADS = 4
- def __init__(self, handler, num_threads=DEFAULT_NUM_THREADS):
- self.num_threads = num_threads
- self.threads = []
- self.queue = queue.Queue()
- self.handler = handler
- def get(self, requests):
- if not self.threads:
- t = threading.Thread(target=self.run)
- t.daemon = True
- t.start()
- self.threads.append(t)
- for r in requests:
- self.queue.put(r)
- def run(self):
- '''called in a thread to process requests by calling the handler'''
- while 1:
- url = self.queue.get()
- self.handler(url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement