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.
master
Ethan Dalool 2020-03-03 01:57:32 -08:00
parent 22321162f4
commit 4a0c0f4b48
1 changed files with 18 additions and 16 deletions

View File

@ -21,7 +21,7 @@ class ThreadPool:
''' '''
paused: paused:
The pool will start in a paused state and you will have to call 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): if not isinstance(size, int):
raise TypeError(f'size must be an int, not {type(size)}.') raise TypeError(f'size must be an int, not {type(size)}.')
@ -36,14 +36,14 @@ class ThreadPool:
def _clear_done_jobs(self): def _clear_done_jobs(self):
''' '''
This function assumes that _job_manager_lock is acquired!! 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}] self._jobs[:] = [j for j in self._jobs if j.status in {PENDING, RUNNING}]
def _start_jobs(self): def _start_jobs(self):
''' '''
This function assumes that _job_manager_lock is acquired!! 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 = self.max_size - self.running_count
available = max(0, available) available = max(0, available)
@ -59,7 +59,7 @@ class ThreadPool:
def _clear_done_and_start_jobs(self): def _clear_done_and_start_jobs(self):
''' '''
This function assumes that _job_manager_lock is acquired!! 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._clear_done_jobs()
self._start_jobs() self._start_jobs()
@ -71,7 +71,9 @@ class ThreadPool:
if self.paused: if self.paused:
return 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 @property
def running_count(self): def running_count(self):
@ -146,7 +148,17 @@ class ThreadPool:
return these_jobs 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. 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._clear_done_and_start_jobs()
self.paused = False 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: class Job:
def __init__(self, pool, function, *, name=None, args=tuple(), kwargs=dict()): def __init__(self, pool, function, *, name=None, args=tuple(), kwargs=dict()):
self.pool = pool self.pool = pool