From 744b0e7c29b4c01e6d1b72dc81eb9a493a4f7f56 Mon Sep 17 00:00:00 2001 From: jay817 Date: Mon, 25 Nov 2024 20:08:33 -0500 Subject: [PATCH] Update nest-asyncio/README.md --- nest-asyncio/README.md | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/nest-asyncio/README.md b/nest-asyncio/README.md index 80a6a02..0cb8404 100644 --- a/nest-asyncio/README.md +++ b/nest-asyncio/README.md @@ -1,19 +1,24 @@ ```python -import asyncio -def _run_once(loop): - loop._process_events(loop._selector.select(0)) - if (ready := loop._ready) and (handle := ready.popleft())._cancelled is False: - task = (tasks := asyncio.tasks._current_tasks).pop(loop, None) - handle._run(); tasks[loop] = task -(loop := asyncio.get_event_loop()).__class__._run_once = _run_once -asyncio.run = loop.run_until_complete; loop.__class__._check_running = lambda _: 0 +def sync(coro): + import asyncio, functools + if not asyncio.iscoroutinefunction(coro): return coro + @functools.wraps(coro) + def wrapper(*args, **kwargs): + loop, future = asyncio.get_event_loop(), asyncio.ensure_future(coro(*args, **kwargs)) + while not future.done(): + loop._process_events(loop._selector.select(0)) + if (ready := loop._ready) and (handle := ready.popleft())._cancelled is False: + task = (tasks := asyncio.tasks._current_tasks).pop(loop, None) + handle._run(); tasks[loop] = task + return future.result() + return wrapper +@sync async def hello(): - print('hello') - async def world(): - print('world') - asyncio.run(world()) -asyncio.run(hello()) + @sync + async def world(): print('hello') + world() or print('world') +hello() """ hello world