forked from p15670423/monkey
Island: Add LockingIslandEventQueueDecorator
This commit is contained in:
parent
67c78abee1
commit
8ee14c4564
|
@ -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
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue