From 1921e620119fb48664d18a0a8c5787ed287e52db Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 12 Feb 2020 19:06:20 -0800 Subject: [PATCH] Add method add_many for batch creation of jobs. --- voussoirkit/threadpool.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/voussoirkit/threadpool.py b/voussoirkit/threadpool.py index e83a8ac..cf0e6f7 100644 --- a/voussoirkit/threadpool.py +++ b/voussoirkit/threadpool.py @@ -114,6 +114,34 @@ class ThreadPool: self.job_manager_lock.release() return job + def add_many(self, kwargss): + ''' + Add multiple new jobs to the pool at once. Useful to prevent the + excessive lock-waiting that you get from calling regular `add` in a + loop while other jobs are finishing and triggering queue maintenance. + + Provide an iterable of kwarg dictionaries. That is: + [ + {'function': print, 'args': [4], 'name': '4'}, + {'function': sample, 'kwargs': {'x': 2}}, + ] + ''' + self.assert_not_closed() + self.job_manager_lock.acquire() + + these_jobs = [] + for kwargs in kwargss: + kwargs.pop('pool', None) + job = Job(pool=self, **kwargs) + these_jobs.append(job) + self.jobs.append(job) + + if not self.paused: + self._clear_done_and_start_jobs() + + self.job_manager_lock.release() + return these_jobs + def clear_done_and_start_jobs(self): ''' Remove finished and raised jobs from the queue and start some new jobs.