From 4a0c0f4b48aef57f0174fe90478bbe29976bb105 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 3 Mar 2020 01:57:32 -0800 Subject: [PATCH] Rename clear_done_and_start_jobs to start. Originally the long name was to make it super clear that by starting the queue you are going to lose whatever is in pool.jobs. But then _jobs became private so the end user shouldn't be touching that anyway. So we're free to make the public interface simpler and just call it start. --- voussoirkit/threadpool.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/voussoirkit/threadpool.py b/voussoirkit/threadpool.py index 76fbd27..2dfa543 100644 --- a/voussoirkit/threadpool.py +++ b/voussoirkit/threadpool.py @@ -21,7 +21,7 @@ class ThreadPool: ''' paused: The pool will start in a paused state and you will have to call - `clear_done_and_start_jobs` to start it. + `start` to start it. ''' if not isinstance(size, int): raise TypeError(f'size must be an int, not {type(size)}.') @@ -36,14 +36,14 @@ class ThreadPool: def _clear_done_jobs(self): ''' This function assumes that _job_manager_lock is acquired!! - You should call clear_done_and_start_jobs instead! + You should call start instead! ''' self._jobs[:] = [j for j in self._jobs if j.status in {PENDING, RUNNING}] def _start_jobs(self): ''' This function assumes that _job_manager_lock is acquired!! - You should call clear_done_and_start_jobs instead! + You should call start instead! ''' available = self.max_size - self.running_count available = max(0, available) @@ -59,7 +59,7 @@ class ThreadPool: def _clear_done_and_start_jobs(self): ''' This function assumes that _job_manager_lock is acquired!! - You should call clear_done_and_start_jobs instead! + You should call start instead! ''' self._clear_done_jobs() self._start_jobs() @@ -71,7 +71,9 @@ class ThreadPool: if self.paused: return - self.clear_done_and_start_jobs() + # Although this method is private, we are calling the public `start` + # instead of the private method because we do not hold the lock. + self.start() @property def running_count(self): @@ -146,7 +148,17 @@ class ThreadPool: return these_jobs - def clear_done_and_start_jobs(self): + def join(self): + ''' + Permanently close the pool, preventing any new jobs from being added, + and block until all jobs are complete. + ''' + self.closed = True + self.start() + for job in self._jobs: + job.join() + + def start(self): ''' Remove finished and raised jobs from the queue and start some new jobs. @@ -162,16 +174,6 @@ class ThreadPool: self._clear_done_and_start_jobs() self.paused = False - def join(self): - ''' - Permanently close the pool, preventing any new jobs from being added, - and block until all jobs are complete. - ''' - self.closed = True - self.clear_done_and_start_jobs() - for job in self._jobs: - job.join() - class Job: def __init__(self, pool, function, *, name=None, args=tuple(), kwargs=dict()): self.pool = pool