-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(robot-sever,api): make ChangeNotifier thread safe (#15089)
Closes EXEC-404
- Loading branch information
Showing
14 changed files
with
85 additions
and
138 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
api/src/opentrons/protocol_engine/state/change_notifier.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Simple state change notification interface.""" | ||
import asyncio | ||
|
||
|
||
class ChangeNotifier: | ||
"""An interface to emit or subscribe to state change notifications.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the ChangeNotifier with an internal Event.""" | ||
self._event = asyncio.Event() | ||
|
||
def notify(self) -> None: | ||
"""Notify all `wait`'ers that the state has changed.""" | ||
self._event.set() | ||
|
||
async def wait(self) -> None: | ||
"""Wait until the next state change notification.""" | ||
self._event.clear() | ||
await self._event.wait() | ||
|
||
|
||
class ChangeNotifier_ts(ChangeNotifier): | ||
"""ChangeNotifier initialized with Event_ts.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the ChangeNotifier_Ts with an internal Event_ts.""" | ||
super().__init__() | ||
self._event = Event_ts() | ||
|
||
|
||
class Event_ts(asyncio.Event): | ||
"""asyncio.Event with threadsafe methods.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize Event_ts with the active event_loop or event_loop_policy if not active.""" | ||
super().__init__() | ||
if self._loop is None: | ||
self._loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() | ||
|
||
def set(self) -> None: | ||
"""Primarily intended for calling from a thread not responsible for the event loop. | ||
Calling set() from the event loop thread will actually delay the execution of the set() until the | ||
calling method either yields, awaits, or exits altogether. This is usually fine but might occasionally cause | ||
unexpected behavior. | ||
""" | ||
self._loop.call_soon_threadsafe(super().set) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
robot-server/robot_server/service/notifications/change_notifier.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
robot-server/tests/service/notifications/test_change_notifier.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.