dls-controls / aioca

Asynchronous Channel Access client for asyncio and Python using libca via ctypes
Apache License 2.0
6 stars 3 forks source link

Dynamically update the subscription list #39

Closed zhangt58 closed 1 year ago

zhangt58 commented 1 year ago

Is that possible to update the subscription list of PVs when a camontior coroutine is already running?

zhangt58 commented 1 year ago

I managed by using asyncio.run_coroutine_threadsafe(), while still hoping if there were any aioca API to change the subscription list interactively.

coretl commented 1 year ago

Interesting idea, what's the use case? There are ways of doing this with aioca at the moment, but no dedicated API.

Under the hood, these are equivalent:

subs = camonitor(pvs, cb)
subs2 = [camonitor(pv, lambda v: cb(v, i) for i, pv in enumerate(pvs)]

Each subscription in the list is independent, the monitoring of a list of pvs is just a shortcut that allows the same callback to be reused with an index added to the call arguments. This means that adding a new pv to the list of subscriptions in the above code could be done like this:

def add_new_subscription(new_pv, subs, cb):
    new_i = len(subs)
    subs.append(camonitor(new_pv, lambda v: cb(v, new_i))

What does your existing code that does this look like? Do you have a proposed API to be added to aioca to make this easier?

zhangt58 commented 1 year ago

I'm trying to use it to interactively manage the PV monitoring subscription process, to support:

All these are to be running on a new thread and communicate with the main thread. The solution right now I have is by subclassing the threading.Thread class, within which I created methods add_pv(), del_pv(), etc to fulfill the abovementioned requirements. And it seems working!

I plan to adopt this solution to GUI apps. For now, I do not have a specific new API to propose to aioca, as it does the job! And the code is really well-written!