-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make devices connect with a timeout (#321)
Co-authored-by: Rose Yemelyanova <rose.yemelyanova@diamond.ac.uk>
- Loading branch information
1 parent
1f9dadf
commit 42cac05
Showing
4 changed files
with
61 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import asyncio | ||
import logging | ||
from contextlib import suppress | ||
from typing import Any, Dict, Iterable | ||
|
||
from ophyd_async.core import DEFAULT_TIMEOUT | ||
from ophyd_async.core import Device as OphydAsyncDevice | ||
from ophyd_async.core import NotConnected | ||
|
||
|
||
async def connect_ophyd_async_devices( | ||
devices: Iterable[Any], | ||
sim: bool = False, | ||
timeout: float = DEFAULT_TIMEOUT, | ||
) -> None: | ||
tasks: Dict[asyncio.Task, str] = {} | ||
for device in devices: | ||
if isinstance(device, OphydAsyncDevice): | ||
task = asyncio.create_task(device.connect(sim=sim)) | ||
tasks[task] = device.name | ||
if tasks: | ||
await _wait_for_tasks(tasks, timeout=timeout) | ||
|
||
|
||
async def _wait_for_tasks(tasks: Dict[asyncio.Task, str], timeout: float): | ||
done, pending = await asyncio.wait(tasks, timeout=timeout) | ||
if pending: | ||
msg = f"{len(pending)} Devices did not connect:" | ||
for t in pending: | ||
t.cancel() | ||
with suppress(Exception): | ||
await t | ||
e = t.exception() | ||
msg += f"\n {tasks[t]}: {type(e).__name__}" | ||
lines = str(e).splitlines() | ||
if len(lines) <= 1: | ||
msg += f": {e}" | ||
else: | ||
msg += "".join(f"\n {line}" for line in lines) | ||
logging.error(msg) | ||
raised = [t for t in done if t.exception()] | ||
if raised: | ||
logging.error(f"{len(raised)} Devices raised an error:") | ||
for t in raised: | ||
logging.exception(f" {tasks[t]}:", exc_info=t.exception()) | ||
if pending or raised: | ||
raise NotConnected("Not all Devices connected") |