Commit 48e05e96 authored by Wout De Nolf's avatar Wout De Nolf
Browse files

Add ThreadCounter

parent 4bfd6860
import logging
from threading import Condition
logger = logging.getLogger("pypushflow")
class ThreadCounter:
"""Scheduling thread counter"""
def __init__(self):
self.__counter = 0
self.__condition = Condition()
def start_thread(self, msg=None):
with self.__condition:
self.__counter += 1
self._log_counter_change(msg=msg)
self.__condition.notify_all()
def end_thread(self, msg=None):
with self.__condition:
self.__counter = max(self.__counter - 1, 0)
self._log_counter_change(msg=msg)
self.__condition.notify_all()
def __enter__(self):
self.start_thread()
return self
def __exit__(self, *args):
self.end_thread()
def wait_threads_finished(self, timeout=None):
"""Returns False when timeout expires"""
while True:
with self.__condition:
if self.__counter == 0:
break
if not self.__condition.wait(timeout=timeout):
return False
return True
@property
def nthreads(self):
return self.__counter
def _log_counter_change(self, msg=None):
if msg is None:
msg = "Thread counter changed"
logger.debug("%s (%d threads running)", msg, self.__counter)
import unittest
from time import sleep
from pypushflow.ThreadCounter import ThreadCounter
from concurrent.futures import ThreadPoolExecutor
class TestThreadCounter(unittest.TestCase):
def setUp(self):
self.counter = ThreadCounter()
def mythread(self, sleep_time):
with self.counter:
sleep(1)
return True
def test_thread_count(self):
self.assertEqual(self.counter.nthreads, 0)
with self.counter as ctr:
self.assertEqual(ctr.nthreads, 1)
with self.counter as ctr:
self.assertEqual(ctr.nthreads, 2)
self.assertEqual(ctr.nthreads, 1)
self.assertEqual(self.counter.nthreads, 0)
def test_multiple_threads(self):
with ThreadPoolExecutor(max_workers=10) as executor:
results = executor.map(self.mythread, [1] * 10)
self.counter.wait_threads_finished()
for result in results:
self.assertTrue(result)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment