Make EventHandlerBase capable of stopping all it's instances

This commit is contained in:
Kristóf Tóth 2019-07-01 16:01:54 +02:00
parent 68a6469d23
commit 3be018eb17
1 changed files with 8 additions and 13 deletions

View File

@ -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)
}