Skip to content

Commit

Permalink
Code review of pelix.threadpool
Browse files Browse the repository at this point in the history
* Corrected format according to PEP-8
* Corrected typo (self.max_threads => self._max_threads)
* Remove "no bytecode" flag
  • Loading branch information
tcalmant committed Aug 19, 2016
1 parent 6948945 commit eaf849c
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions pelix/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from sys import dont_write_bytecode
dont_write_bytecode

# Standard library
import logging
Expand Down Expand Up @@ -214,7 +212,8 @@ def __init__(self, max_threads, min_threads=1, queue_size=0, timeout=60,
# Thread count
self._thread_id = 0

# Current number of threads, active and alive, and number of task waiting
# Current number of threads, active and alive,
# and number of task waiting
self.__nb_threads = 0
self.__nb_active_threads = 0
self.__nb_pending_task = 0
Expand All @@ -234,20 +233,19 @@ def start(self):
nb_pending_tasks = self._queue.qsize()
if nb_pending_tasks > self._max_threads:
nb_threads = self._max_threads
nb_pending_tasks = self.max_threads
nb_pending_tasks = self._max_threads
elif nb_pending_tasks < self._min_threads:
nb_threads = self._min_threads
else:
nb_threads = nb_pending_tasks

# Create the threads
for _ in range(0,nb_pending_tasks):
for _ in range(nb_pending_tasks):
self.__nb_pending_task += 1
self.__start_thread()
for _ in range(0,nb_threads-nb_pending_tasks):
for _ in range(nb_threads-nb_pending_tasks):
self.__start_thread()



def __start_thread(self):
"""
Starts a new thread, if possible
Expand Down Expand Up @@ -335,7 +333,7 @@ def enqueue(self, method, *args, **kwargs):
self._queue.put((method, args, kwargs, future), True,
self._timeout)
self.__nb_pending_task += 1

if self.__nb_pending_task > self.__nb_threads:
# All threads are taken: start a new one
self.__start_thread()
Expand Down Expand Up @@ -384,7 +382,6 @@ def __run(self):
"""
The main loop
"""

while not self._done_event.is_set():
try:
# Wait for an action (blocking)
Expand Down Expand Up @@ -420,12 +417,14 @@ def __run(self):

# Clean up thread if necessary
with self.__lock:
if self.__nb_threads > self._min_threads and self.__nb_threads - self.__nb_active_threads > self._queue.qsize():
extra_threads = self.__nb_threads - self.__nb_active_threads
if self.__nb_threads > self._min_threads \
and extra_threads > self._queue.qsize():
# No more work for this thread
# if there are more non active_thread than task
# and we're above the minimum number of threads: stop this one
# and we're above the minimum number of threads:
# stop this one
self.__nb_threads -= 1
#print("fin lock remaining threads {}".format(self.__nb_threads))
return

with self.__lock:
Expand Down

0 comments on commit eaf849c

Please sign in to comment.