bluesky / ophyd-async

Hardware abstraction for bluesky written using asyncio
https://blueskyproject.io/ophyd-async
BSD 3-Clause "New" or "Revised" License
11 stars 25 forks source link

Discussion: Device Collector #112

Open callumforrester opened 9 months ago

callumforrester commented 9 months ago

Background

DeviceCollector is an context manager that can be used to connect one or more devices to their backends in an easy-to-write way:

# Outside a running event loop
with DeviceCollector():
    x = MyMotor(...)
    y = MyMotor(...)
    det = MyDetector(...)

# Inside a running event loop
async with DeviceCollector():
    x = MyMotor(...)
    y = MyMotor(...)
    det = MyDetector(...)

Because it is ophyd-async, a running event loop is actually needed in both cases. The outside case is for when there is already an event loop running in the background, which will have been created by the RunEngine constructor when, for example, you have a RunEngine in an IPython terminal.

This introduces two potential failure cases:

Currently we are providing a totally RunEngine-independent route to making and controlling ophyd-async devices (currently async with) and an async-free route which annoyingly requires a RunEngine (with).

Finally, in the interest of modularity, it is desirable to minimize or even remove ophyd-async's dependency on bluesky.

First Steps

The first step is to improve the error message in the first failure case so the user is at least informed that they need to make a RunEngine first. Ideally the message should point to a docs page that explains these nuances and what the two possible context managers give you.

Other Solutions Discussed

callumforrester commented 9 months ago

Tagging @DominicOram and @olliesilvester for interest

callumforrester commented 9 months ago

Relates to #81