-
Notifications
You must be signed in to change notification settings - Fork 2
WIP feat: lock devices during a scan #733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d-perl
wants to merge
6
commits into
main
Choose a base branch
from
feat/device_locking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−104
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,58 @@ | ||
| import threading | ||
| from contextlib import contextmanager | ||
| from typing import Dict, Iterable | ||
|
|
||
| from bec_lib.logger import bec_logger | ||
|
|
||
| logger = bec_logger.logger | ||
|
|
||
|
|
||
| class DeviceLockManager: | ||
| """ | ||
| Manages locks for devices, identified simply as their name. | ||
| Allows acquiring multiple item locks atomically via a context manager. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self._locks: Dict[str, threading.RLock] = {} | ||
| self._locks_guard = threading.RLock() | ||
|
|
||
| def _get_lock(self, key: str) -> threading.RLock: | ||
| """ | ||
| Get (or create) a lock for a given key. | ||
| """ | ||
| with self._locks_guard: | ||
| if key not in self._locks: | ||
| self._locks[key] = threading.RLock() | ||
| return self._locks[key] | ||
|
|
||
| @contextmanager | ||
| def lock(self, keys: Iterable[str], blocking: bool = True): | ||
| """ | ||
| Context manager to lock one or more items. | ||
| """ | ||
| keys = list(set(keys)) | ||
| try: | ||
| if not self.acquire(*keys, blocking=blocking): | ||
| return | ||
| yield | ||
| finally: | ||
| self.release(*keys) | ||
|
|
||
| def acquire(self, *keys: str, blocking: bool = True): | ||
| logger.info(f"Locking devices: {keys}") | ||
| with self._locks_guard: | ||
| new_locks = [] | ||
| for key in sorted(keys): | ||
| next_lock = self._get_lock(key) | ||
| if not next_lock.acquire(blocking=blocking): | ||
| [lock.release() for lock in new_locks] | ||
| return False | ||
| new_locks.append(next_lock) | ||
| return True | ||
|
|
||
| def release(self, *keys: str): | ||
| logger.info(f"Releasing devices: {keys}") | ||
| with self._locks_guard: | ||
| for key in reversed(sorted(keys)): | ||
| self._get_lock(key).release() |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| from typing import Callable | ||
|
|
||
| import pytest | ||
| from bec_server.scan_server.scan_queue import QueueManager | ||
|
|
||
| from bec_lib import messages | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def qm_with_3_qs_and_lock_man(queuemanager_mock: Callable[..., QueueManager]): | ||
| queue_manager = queuemanager_mock(["1", "2", "3"]) | ||
| yield queue_manager, queue_manager.parent.device_locks | ||
|
|
||
|
|
||
| def _linescan_msg(dev: str, start: float, stop: float): | ||
| return messages.ScanQueueMessage( | ||
| scan_type="line_scan", | ||
| parameter={"args": {dev: (start, stop)}, "kwargs": {}}, | ||
| queue="primary", | ||
| metadata={"RID": "something"}, | ||
| ) | ||
|
|
||
|
|
||
| def test_devices_from_instance(queuemanager_mock): | ||
| q_manager = queuemanager_mock() | ||
| assembler = q_manager.parent.scan_assembler | ||
| scan_instance = assembler.assemble_device_instructions(_linescan_msg("samx", -1, 1), "test") | ||
| device_access = scan_instance.instance_device_access() | ||
| assert device_access.device_locking == set(("samx",)) | ||
|
|
||
|
|
||
| def test_queuemanager_add_to_queue_restarts_queue_if_worker_is_dead(qm_with_3_qs_and_lock_man): | ||
| queue_manager, locks = qm_with_3_qs_and_lock_man | ||
| msg = _linescan_msg("samx", -5, 5) | ||
|
|
||
| queue_manager.add_to_queue(scan_queue="1", msg=msg) | ||
|
|
||
| ... |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.