Island: Add LockingIslandEventQueueDecorator

This commit is contained in:
Mike Salvatore 2022-09-29 11:55:47 -04:00
parent 67c78abee1
commit 8ee14c4564
2 changed files with 21 additions and 0 deletions

View File

@ -1,3 +1,4 @@
from .types import IslandEventSubscriber
from .i_island_event_queue import IIslandEventQueue, IslandEventTopic
from .pypubsub_island_event_queue import PyPubSubIslandEventQueue
from .locking_island_event_queue_decorator import LockingIslandEventQueueDecorator

View File

@ -0,0 +1,20 @@
from threading import Lock
from . import IIslandEventQueue, IslandEventSubscriber, IslandEventTopic
class LockingIslandEventQueueDecorator(IIslandEventQueue):
"""
Makes an IIslandEventQueue thread-safe by locking publish()
"""
def __init__(self, island_event_queue: IIslandEventQueue):
self._lock = Lock()
self._island_event_queue = island_event_queue
def subscribe(self, topic: IslandEventTopic, subscriber: IslandEventSubscriber):
self._island_event_queue.subscribe(topic, subscriber)
def publish(self, topic: IslandEventTopic, **kwargs):
with self._lock:
self._island_event_queue.publish(topic, **kwargs)