Files
wiki/nest-asyncio/README.md
2024-11-25 14:05:41 -05:00

592 B

import asyncio
(loop := asyncio.get_event_loop()).__class__._check_running = lambda _: None
def _run_once(loop):
    loop._process_events(loop._selector.select(0))
    if (ready := loop._ready) and not (handle := ready.popleft())._cancelled:
        task = (tasks := asyncio.tasks._current_tasks).pop(loop, None)
        handle._run()
        tasks[loop] = task
loop.__class__._run_once = _run_once
asyncio.run = loop.run_until_complete

async def hello():
    async def world(): print('hello')
    asyncio.run(world()) or print('world')
asyncio.run(hello())
"""
hello
world
"""