Skip to content
Snippets Groups Projects
Commit 97c4dfe2 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

fixed thread

parent c7932f10
No related branches found
No related tags found
No related merge requests found
import concurrent
import ctypes
import platform
import threading
......@@ -189,6 +188,10 @@ class TerminableThread(threading.Thread):
"""
def __init__(self, *args, **kwargs):
"""
Note that this is called in the constructor's thread. Use .prepare() to
run statements that should be ran in new thread.
"""
super().__init__(*args, **kwargs)
self._terminating = False # type: bool
......@@ -197,6 +200,13 @@ class TerminableThread(threading.Thread):
"""Return whether a termination of this thread was requested"""
return self._terminating
def prepare(self) -> None:
"""
This is called before the .loop() looping loop is entered.
This is invoked already in a separate thread.
"""
def loop(self) -> None:
"""
Run one iteration of the loop. Meant to be overrided. You do not need to override it
......@@ -218,6 +228,7 @@ class TerminableThread(threading.Thread):
"""
Calls self.loop() indefinitely, until terminating condition is met
"""
self.prepare()
while not self._terminating:
self.loop()
self.cleanup()
......
......@@ -260,7 +260,11 @@ class TestConcurrent(unittest.TestCase):
def test_terminable_thread(self):
class MyTerminableThread(TerminableThread):
def prepare(self):
self.a = 5
def loop(self):
self.a += 1
time.sleep(0.5)
mtt = MyTerminableThread()
......@@ -270,6 +274,7 @@ class TestConcurrent(unittest.TestCase):
@unittest.skipIf(platform.python_implementation() != 'PyPy', 'this requires PyPy')
def test_terminable_thread_force_notimplementederror(self):
class MyTerminableThread(TerminableThread):
def run(self):
a = 0
while not self.terminating:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment