From 46e6e82fa0f471b1f4e4ce68d2b399e42ba39731 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 3 Mar 2020 01:55:22 -0800 Subject: [PATCH] Make running_count and unfinished_count @property. --- voussoirkit/threadpool.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/voussoirkit/threadpool.py b/voussoirkit/threadpool.py index 66d2069..8d7fe9b 100644 --- a/voussoirkit/threadpool.py +++ b/voussoirkit/threadpool.py @@ -45,7 +45,7 @@ class ThreadPool: This function assumes that _job_manager_lock is acquired!! You should call clear_done_and_start_jobs instead! ''' - available = self.max_size - self.running_count() + available = self.max_size - self.running_count available = max(0, available) if available == 0: return @@ -75,6 +75,14 @@ class ThreadPool: self.clear_done_and_start_jobs() + @property + def running_count(self): + return sum(1 for job in list(self._jobs) if job.status is RUNNING) + + @property + def unfinished_count(self): + return sum(1 for job in list(self._jobs) if job.status in {PENDING, RUNNING}) + def assert_not_closed(self): ''' If the pool is closed (because you called `join`), raise PoolClosed. @@ -166,12 +174,6 @@ class ThreadPool: for job in self._jobs: job.join() - def running_count(self): - return sum(1 for job in list(self._jobs) if job.status is RUNNING) - - def unfinished_count(self): - return sum(1 for job in list(self._jobs) if job.status in {PENDING, RUNNING}) - class Job: def __init__(self, pool, function, *, name=None, args=tuple(), kwargs=dict()): self.pool = pool