11import asyncio
22from collections import defaultdict
33from collections .abc import Callable
4+ from concurrent .futures import Future
45from types import MethodType
56
67from softioc .asyncio_dispatcher import AsyncioDispatcher
@@ -19,8 +20,8 @@ def __init__(
1920 self ._loop = self ._dispatcher .loop
2021 self ._controller = controller
2122
22- self ._initial_tasks = [controller .connect ]
23- self ._scan_tasks : list [ asyncio . Task ] = []
23+ self ._initial_coros = [controller .connect ]
24+ self ._scan_futures : set [ Future ] = set ()
2425
2526 asyncio .run_coroutine_threadsafe (
2627 self ._controller .initialise (), self ._loop
@@ -41,28 +42,29 @@ def _link_process_tasks(self):
4142 _link_attribute_sender_class (single_mapping )
4243
4344 def __del__ (self ):
44- self .stop_scan_tasks ()
45+ self .stop_scan_futures ()
4546
4647 def run (self ):
47- self ._run_initial_tasks ()
48- self .start_scan_tasks ()
48+ self ._run_initial_futures ()
49+ self .start_scan_futures ()
4950 self ._run ()
5051
51- def _run_initial_tasks (self ):
52- for task in self ._initial_tasks :
53- future = asyncio .run_coroutine_threadsafe (task (), self ._loop )
52+ def _run_initial_futures (self ):
53+ for coro in self ._initial_coros :
54+ future = asyncio .run_coroutine_threadsafe (coro (), self ._loop )
5455 future .result ()
5556
56- def start_scan_tasks (self ):
57- self ._scan_tasks = [
58- self ._loop .create_task (coro ()) for coro in _get_scan_coros (self ._mapping )
59- ]
57+ def start_scan_futures (self ):
58+ self ._scan_futures = {
59+ asyncio .run_coroutine_threadsafe (coro (), self ._loop )
60+ for coro in _get_scan_coros (self ._mapping )
61+ }
6062
61- def stop_scan_tasks (self ):
62- for task in self ._scan_tasks :
63- if not task .done ():
63+ def stop_scan_futures (self ):
64+ for future in self ._scan_futures :
65+ if not future .done ():
6466 try :
65- task .cancel ()
67+ future .cancel ()
6668 except asyncio .CancelledError :
6769 pass
6870
0 commit comments