11import asyncio
22from collections import defaultdict
33from collections .abc import Callable
4- from concurrent .futures import Future
54from types import MethodType
65
7- from softioc .asyncio_dispatcher import AsyncioDispatcher
8-
96from .attributes import AttrR , AttrW , Sender , Updater
107from .controller import Controller , SingleMapping
118from .exceptions import FastCSException
@@ -15,19 +12,15 @@ class Backend:
1512 def __init__ (
1613 self ,
1714 controller : Controller ,
18- loop : asyncio .AbstractEventLoop | None = None ,
15+ loop : asyncio .AbstractEventLoop ,
1916 ):
20- self .dispatcher = AsyncioDispatcher (loop )
21- self ._loop = self .dispatcher .loop
17+ self ._loop = loop
2218 self ._controller = controller
2319
2420 self ._initial_coros = [controller .connect ]
25- self ._scan_futures : set [Future ] = set ()
26-
27- asyncio .run_coroutine_threadsafe (
28- self ._controller .initialise (), self ._loop
29- ).result ()
21+ self ._scan_tasks : set [asyncio .Task ] = set ()
3022
23+ loop .run_until_complete (self ._controller .initialise ())
3124 self ._link_process_tasks ()
3225
3326 def _link_process_tasks (self ):
@@ -36,28 +29,26 @@ def _link_process_tasks(self):
3629 _link_attribute_sender_class (single_mapping )
3730
3831 def __del__ (self ):
39- self .stop_scan_futures ()
32+ self ._stop_scan_tasks ()
4033
41- def run (self ):
42- self ._run_initial_futures ()
43- self .start_scan_futures ()
34+ async def serve (self ):
35+ await self ._run_initial_tasks ()
36+ await self ._start_scan_tasks ()
4437
45- def _run_initial_futures (self ):
38+ async def _run_initial_tasks (self ):
4639 for coro in self ._initial_coros :
47- future = asyncio .run_coroutine_threadsafe (coro (), self ._loop )
48- future .result ()
40+ await coro ()
4941
50- def start_scan_futures (self ):
51- self ._scan_futures = {
52- asyncio .run_coroutine_threadsafe (coro (), self ._loop )
53- for coro in _get_scan_coros (self ._controller )
42+ async def _start_scan_tasks (self ):
43+ self ._scan_tasks = {
44+ self ._loop .create_task (coro ()) for coro in _get_scan_coros (self ._controller )
5445 }
5546
56- def stop_scan_futures (self ):
57- for future in self ._scan_futures :
58- if not future .done ():
47+ def _stop_scan_tasks (self ):
48+ for task in self ._scan_tasks :
49+ if not task .done ():
5950 try :
60- future .cancel ()
51+ task .cancel ()
6152 except asyncio .CancelledError :
6253 pass
6354
@@ -83,9 +74,9 @@ def _link_attribute_sender_class(single_mapping: SingleMapping) -> None:
8374 for attr_name , attribute in single_mapping .attributes .items ():
8475 match attribute :
8576 case AttrW (sender = Sender ()):
86- assert (
87- not attribute . has_process_callback ()
88- ), f"Cannot assign both put method and Sender object to { attr_name } "
77+ assert not attribute . has_process_callback (), (
78+ f"Cannot assign both put method and Sender object to { attr_name } "
79+ )
8980
9081 callback = _create_sender_callback (attribute , single_mapping .controller )
9182 attribute .set_process_callback (callback )
0 commit comments