Use with statement for _job_manager_lock.

This commit is contained in:
Ethan Dalool 2020-02-12 19:16:35 -08:00
parent f6872e77cf
commit 64d0968040

View file

@ -97,21 +97,20 @@ class ThreadPool:
a human friendly name string. a human friendly name string.
''' '''
self.assert_not_closed() self.assert_not_closed()
self._job_manager_lock.acquire()
job = Job( with self._job_manager_lock:
pool=self, job = Job(
function=function, pool=self,
name=name, function=function,
args=args, name=name,
kwargs=kwargs, args=args,
) kwargs=kwargs,
self._jobs.append(job) )
self._jobs.append(job)
if not self.paused: if not self.paused:
self._clear_done_and_start_jobs() self._clear_done_and_start_jobs()
self._job_manager_lock.release()
return job return job
def add_many(self, kwargss): def add_many(self, kwargss):
@ -127,19 +126,18 @@ class ThreadPool:
] ]
''' '''
self.assert_not_closed() self.assert_not_closed()
self._job_manager_lock.acquire()
these_jobs = [] with self._job_manager_lock:
for kwargs in kwargss: these_jobs = []
kwargs.pop('pool', None) for kwargs in kwargss:
job = Job(pool=self, **kwargs) kwargs.pop('pool', None)
these_jobs.append(job) job = Job(pool=self, **kwargs)
self._jobs.append(job) these_jobs.append(job)
self._jobs.append(job)
if not self.paused: if not self.paused:
self._clear_done_and_start_jobs() self._clear_done_and_start_jobs()
self._job_manager_lock.release()
return these_jobs return these_jobs
def clear_done_and_start_jobs(self): def clear_done_and_start_jobs(self):
@ -154,10 +152,9 @@ class ThreadPool:
Because the pool's internal job queue is flushed regularly, you should Because the pool's internal job queue is flushed regularly, you should
store your own references to jobs to get their return values. store your own references to jobs to get their return values.
''' '''
self._job_manager_lock.acquire() with self._job_manager_lock:
self._clear_done_and_start_jobs() self._clear_done_and_start_jobs()
self.paused = False self.paused = False
self._job_manager_lock.release()
def join(self): def join(self):
''' '''