lib.AsyncRunner: Fix asyncio.Event() for Python 3.12+ #61
1 changed files with 10 additions and 12 deletions
|
|
@ -17,25 +17,23 @@ def loop_in_thread() -> Generator[asyncio.AbstractEventLoop, None, None]:
|
||||||
loop_fut: concurrent.futures.Future[asyncio.AbstractEventLoop] = (
|
loop_fut: concurrent.futures.Future[asyncio.AbstractEventLoop] = (
|
||||||
concurrent.futures.Future()
|
concurrent.futures.Future()
|
||||||
)
|
)
|
||||||
|
stop_fut: concurrent.futures.Future[asyncio.Event] = (concurrent.futures.Future())
|
||||||
stop_event = asyncio.Event()
|
|
||||||
|
|
||||||
async def main() -> None:
|
async def main() -> None:
|
||||||
loop_fut.set_result(asyncio.get_running_loop())
|
loop_fut.set_result(asyncio.get_running_loop())
|
||||||
|
stop_event = asyncio.Event()
|
||||||
|
stop_fut.set_result(stop_event)
|
||||||
await stop_event.wait()
|
await stop_event.wait()
|
||||||
|
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers = 1) as tpe:
|
with concurrent.futures.ThreadPoolExecutor(max_workers = 1) as tpe:
|
||||||
complete_fut = tpe.submit(asyncio.run, main())
|
complete_fut = tpe.submit(asyncio.run, main())
|
||||||
|
loop = loop_fut.result()
|
||||||
for fut in concurrent.futures.as_completed((loop_fut, complete_fut)):
|
stop_event = stop_fut.result()
|
||||||
if fut is loop_fut:
|
try:
|
||||||
loop = loop_fut.result()
|
yield loop
|
||||||
try:
|
finally:
|
||||||
yield loop
|
loop.call_soon_threadsafe(stop_event.set)
|
||||||
finally:
|
complete_fut.result()
|
||||||
loop.call_soon_threadsafe(stop_event.set)
|
|
||||||
else:
|
|
||||||
fut.result()
|
|
||||||
|
|
||||||
class AsyncRunner:
|
class AsyncRunner:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue