lib.AsyncRunner: Fix asyncio.Event() for Python 3.12+ #61

Merged
Jan Lindemann merged 1 commit from jan/feature/20260815-lib-asyncrunner-fix-asyncio-event-for-python into master 2026-08-15 20:00:30 +02:00 AGit

View file

@ -17,25 +17,23 @@ def loop_in_thread() -> Generator[asyncio.AbstractEventLoop, None, None]:
loop_fut: concurrent.futures.Future[asyncio.AbstractEventLoop] = (
concurrent.futures.Future()
)
stop_event = asyncio.Event()
stop_fut: concurrent.futures.Future[asyncio.Event] = (concurrent.futures.Future())
async def main() -> None:
loop_fut.set_result(asyncio.get_running_loop())
stop_event = asyncio.Event()
stop_fut.set_result(stop_event)
await stop_event.wait()
with concurrent.futures.ThreadPoolExecutor(max_workers = 1) as tpe:
complete_fut = tpe.submit(asyncio.run, main())
for fut in concurrent.futures.as_completed((loop_fut, complete_fut)):
if fut is loop_fut:
loop = loop_fut.result()
try:
yield loop
finally:
loop.call_soon_threadsafe(stop_event.set)
else:
fut.result()
loop = loop_fut.result()
stop_event = stop_fut.result()
try:
yield loop
finally:
loop.call_soon_threadsafe(stop_event.set)
complete_fut.result()
class AsyncRunner: