From 3be018eb17ab335ce3368aa5b6b1a03c9edbfe82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 1 Jul 2019 16:01:54 +0200 Subject: [PATCH] Make EventHandlerBase capable of stopping all it's instances --- lib/tfw/event_handlers/event_handler_base.py | 21 ++++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/tfw/event_handlers/event_handler_base.py b/lib/tfw/event_handlers/event_handler_base.py index ecf944b..051363e 100644 --- a/lib/tfw/event_handlers/event_handler_base.py +++ b/lib/tfw/event_handlers/event_handler_base.py @@ -1,5 +1,4 @@ import logging -from inspect import currentframe from typing import Iterable LOG = logging.getLogger(__name__) @@ -12,7 +11,10 @@ class EventHandlerBase: Derived classes must implement the handle_event() method """ + _instances = set() + def __init__(self, key, server_connector, scope): + type(self)._instances.add(self) self.server_connector = server_connector self.scope = scope self.keys = [] @@ -92,6 +94,11 @@ class EventHandlerBase: self.server_connector.unsubscribe(key) self.keys.remove(key) + @classmethod + def stop_all_instances(cls): + for instance in cls._instances: + instance.stop() + def stop(self): self.server_connector.close() self.cleanup() @@ -101,15 +108,3 @@ class EventHandlerBase: Perform cleanup actions such as releasing database connections and stuff like that. """ - - @classmethod - def get_local_instances(cls): - frame = currentframe() - if frame is None: - raise EnvironmentError('inspect.currentframe() is not supported!') - - locals_values = frame.f_back.f_locals.values() - return { - instance for instance in locals_values - if isinstance(instance, cls) - }