From 9781644e7ce067d4ee36bd701c4d58329f031136 Mon Sep 17 00:00:00 2001 From: Collin Dutter Date: Wed, 11 Sep 2024 09:21:44 -0700 Subject: [PATCH] Support using multiple/concurrent EventListeners --- griptape/events/event_bus.py | 5 +++-- tests/unit/events/test_event_listener.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/griptape/events/event_bus.py b/griptape/events/event_bus.py index b7954480e1..f6d797473c 100644 --- a/griptape/events/event_bus.py +++ b/griptape/events/event_bus.py @@ -24,8 +24,9 @@ def add_event_listeners(self, event_listeners: list[EventListener]) -> list[Even return [self.add_event_listener(event_listener) for event_listener in event_listeners] def remove_event_listeners(self, event_listeners: list[EventListener]) -> None: - for event_listener in event_listeners: - self.remove_event_listener(event_listener) + with self._thread_lock: + for event_listener in event_listeners: + self.remove_event_listener(event_listener) def add_event_listener(self, event_listener: EventListener) -> EventListener: with self._thread_lock: diff --git a/tests/unit/events/test_event_listener.py b/tests/unit/events/test_event_listener.py index f35bc5416f..6af1213b16 100644 --- a/tests/unit/events/test_event_listener.py +++ b/tests/unit/events/test_event_listener.py @@ -94,8 +94,8 @@ def test_add_remove_event_listener(self, pipeline): mock1 = Mock() mock2 = Mock() # duplicate event listeners will only get added once - event_listener_1 = EventBus.add_event_listener(EventListener(mock1, event_types=[StartPromptEvent])) - EventBus.add_event_listener(EventListener(mock1, event_types=[StartPromptEvent])) + event_listener_1 = EventBus.add_event_listener(EventListener(mock1, id="1", event_types=[StartPromptEvent])) + EventBus.add_event_listener(EventListener(mock1, id="1", event_types=[StartPromptEvent])) event_listener_3 = EventBus.add_event_listener(EventListener(mock1, event_types=[FinishPromptEvent])) event_listener_4 = EventBus.add_event_listener(EventListener(mock2, event_types=[StartPromptEvent]))