| f | import asyncio | f | import asyncio |
| | | |
| class Loop: | | class Loop: |
| n | _tasks = [] | n | _registry = [] |
| _pointer = 0 | | _current = 0 |
| _halted = False | | _finished = False |
| | | |
| def __init__(self): | | def __init__(self): |
| pass | | pass |
| | | |
| n | def __call__(self, routine): | n | def __call__(self, coro): |
| slot = len(Loop._tasks) | | index = len(Loop._registry) |
| Loop._tasks.append(None) | | Loop._registry.append(None) |
| | | |
| n | async def envelope(*a, **kw): | n | async def wrapper(*args, **kwargs): |
| while True: | | while True: |
| n | if Loop._halted: | n | if Loop._finished: |
| return None | | return None |
| n | if Loop._pointer != slot: | n | if Loop._current != index: |
| await asyncio.sleep(0) | | await asyncio.sleep(0) |
| continue | | continue |
| n | outcome = await routine(*a, **kw) | n | res = await coro(*args, **kwargs) |
| if outcome is None: | | if res is None: |
| Loop._halted = True | | Loop._finished = True |
| Loop._pointer = (Loop._pointer + 1) % len(Loop._tasks) | | Loop._current = (Loop._current + 1) % len(Loop._registry |
| | | ) |
| if outcome is None: | | if res is None: |
| return None | | return None |
| t | return envelope | t | return wrapper |