Update nest-asyncio/README.md
This commit is contained in:
@@ -1,19 +1,24 @@
|
|||||||
```python
|
```python
|
||||||
import asyncio
|
def sync(coro):
|
||||||
def _run_once(loop):
|
import asyncio, functools
|
||||||
loop._process_events(loop._selector.select(0))
|
if not asyncio.iscoroutinefunction(coro): return coro
|
||||||
if (ready := loop._ready) and (handle := ready.popleft())._cancelled is False:
|
@functools.wraps(coro)
|
||||||
task = (tasks := asyncio.tasks._current_tasks).pop(loop, None)
|
def wrapper(*args, **kwargs):
|
||||||
handle._run(); tasks[loop] = task
|
loop, future = asyncio.get_event_loop(), asyncio.ensure_future(coro(*args, **kwargs))
|
||||||
(loop := asyncio.get_event_loop()).__class__._run_once = _run_once
|
while not future.done():
|
||||||
asyncio.run = loop.run_until_complete; loop.__class__._check_running = lambda _: 0
|
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():
|
async def hello():
|
||||||
print('hello')
|
@sync
|
||||||
async def world():
|
async def world(): print('hello')
|
||||||
print('world')
|
world() or print('world')
|
||||||
asyncio.run(world())
|
hello()
|
||||||
asyncio.run(hello())
|
|
||||||
"""
|
"""
|
||||||
hello
|
hello
|
||||||
world
|
world
|
||||||
|
|||||||
Reference in New Issue
Block a user