Skip to content

Commit 2fcb586

Browse files
committed
update: use Event as stop trigger
1 parent c98afcf commit 2fcb586

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/os_3m_engine/core/othread.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
class Othread(Configurable, threading.Thread):
1010

1111
def __init__(self, engine_config, runtime_context,
12-
start_trigger, component_factory, **kwargs):
12+
component_factory, start_trigger, stop_trigger, **kwargs):
1313
Configurable.__init__(self, engine_config)
1414
threading.Thread.__init__(self, **kwargs)
1515
self._driver_cls = load_class(engine_config.driver_cls, Driver)
1616
self._component_factory = component_factory
1717
self._runtime_context = runtime_context
18-
self._stopping = False
18+
self._stop_trigger = stop_trigger
1919
self._start_trigger = start_trigger
2020
self._logger = logging.getLogger(self.name)
2121

@@ -39,23 +39,24 @@ def run(self):
3939
driver.cleanup()
4040
except Exception as e:
4141
self._logger.error('Unexpected exception, %s' % str(e))
42+
self._stop_trigger.set()
4243
self._logger.debug('Stop')
4344

4445
def stopping(self):
45-
return self._stopping
46-
47-
def stop(self):
48-
self._stopping = True
46+
return self._stop_trigger.is_set()
4947

5048

5149
class OthreadManager(Configurable):
5250
def __init__(self, engine_config, runtime_context, component_factory):
5351
super(OthreadManager, self).__init__(engine_config)
5452
start_trigger = threading.Event()
53+
stop_trigger = threading.Event()
5554
self._start_trigger = start_trigger
55+
self._stop_trigger = stop_trigger
5656
thread_cls = load_class(engine_config.thread_cls, Othread)
5757
self._threads = [thread_cls(
58-
engine_config, runtime_context, start_trigger, component_factory,
58+
engine_config, runtime_context, component_factory,
59+
start_trigger, stop_trigger,
5960
name='%s-%d' % (thread_cls.__name__, i))
6061
for i in range(0, engine_config.thread_num)]
6162

@@ -76,7 +77,7 @@ def join(self):
7677
list(map(lambda t: t.join(), self._threads))
7778

7879
def stop(self):
79-
list(map(lambda t: t.stop(), self._threads))
80+
self._stop_trigger.set()
8081

8182
def stopped(self):
8283
return not any([t.isAlive() for t in self._threads])

0 commit comments

Comments
 (0)