Complain if too much jobs queued.
This commit is contained in:
parent
c438f35bcd
commit
e586d8debc
2 changed files with 58 additions and 9 deletions
|
@ -89,10 +89,11 @@ class ThreadPool(object):
|
|||
# pylint: disable=too-few-public-methods
|
||||
def __init__(self, worker_count, job_handler):
|
||||
queue = self.queue = Queue.Queue()
|
||||
current_jobs = self.current_jobs = []
|
||||
|
||||
for _ in xrange(worker_count):
|
||||
worker = threading.Thread(target=_threadpool_worker,
|
||||
args=(queue, job_handler))
|
||||
args=(queue, current_jobs, job_handler))
|
||||
worker.daemon = True
|
||||
worker.start()
|
||||
|
||||
|
@ -101,9 +102,21 @@ class ThreadPool(object):
|
|||
self.queue.put(args)
|
||||
|
||||
|
||||
def _threadpool_worker(queue, job_handler):
|
||||
def _threadpool_worker(queue, current_jobs, job_handler):
|
||||
""" Provides the base functionality of a worker for the thread pool. """
|
||||
while True:
|
||||
# Get new item from queue
|
||||
job = queue.get()
|
||||
|
||||
# Add to current running jobs
|
||||
job_log = (datetime.datetime.now(), job)
|
||||
current_jobs.append(job_log)
|
||||
|
||||
# Do the job
|
||||
job_handler(job)
|
||||
|
||||
# Remove from current running job
|
||||
current_jobs.remove(job_log)
|
||||
|
||||
# Tell queue a task is done
|
||||
queue.task_done()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue