Release storico UDC e picking list

This commit is contained in:
2026-06-03 11:41:25 +02:00
parent 4dabba8ce7
commit 742f6a9fe9
28 changed files with 2021 additions and 42 deletions

View File

@@ -7,6 +7,7 @@ singleton so every module can schedule work on the same async runtime.
import asyncio
import threading
import contextlib
class _LoopHolder:
@@ -15,6 +16,7 @@ class _LoopHolder:
def __init__(self):
self.loop = None
self.thread = None
self.closing = False
_GLOBAL = _LoopHolder()
@@ -26,7 +28,7 @@ def get_global_loop() -> asyncio.AbstractEventLoop:
The loop is created lazily the first time the function is called and kept
alive for the lifetime of the application.
"""
if _GLOBAL.loop:
if _GLOBAL.loop and not _GLOBAL.closing:
return _GLOBAL.loop
ready = threading.Event()
@@ -35,7 +37,23 @@ def get_global_loop() -> asyncio.AbstractEventLoop:
_GLOBAL.loop = asyncio.new_event_loop()
asyncio.set_event_loop(_GLOBAL.loop)
ready.set()
_GLOBAL.loop.run_forever()
try:
_GLOBAL.loop.run_forever()
finally:
loop = _GLOBAL.loop
if loop is not None:
pending = [task for task in asyncio.all_tasks(loop) if not task.done()]
for task in pending:
task.cancel()
if pending:
with contextlib.suppress(Exception):
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
with contextlib.suppress(Exception):
loop.run_until_complete(loop.shutdown_asyncgens())
with contextlib.suppress(Exception):
loop.run_until_complete(loop.shutdown_default_executor())
with contextlib.suppress(Exception):
loop.close()
_GLOBAL.thread = threading.Thread(target=_run, name="asyncio-bg-loop", daemon=True)
_GLOBAL.thread.start()
@@ -46,7 +64,9 @@ def get_global_loop() -> asyncio.AbstractEventLoop:
def stop_global_loop():
"""Stop the shared loop and join the background thread if present."""
if _GLOBAL.loop and _GLOBAL.loop.is_running():
_GLOBAL.closing = True
_GLOBAL.loop.call_soon_threadsafe(_GLOBAL.loop.stop)
_GLOBAL.thread.join(timeout=2)
_GLOBAL.loop = None
_GLOBAL.thread = None
_GLOBAL.closing = False