| f | import asyncio | f | import asyncio |
| | | |
| class Loop: | | class Loop: |
| n | _coroutines = [] | n | _registry = [] |
| _step_idx = 0 | | _current = 0 |
| _stop_flag = False | | _finished = False |
| | | |
| def __init__(self): | | def __init__(self): |
| pass | | pass |
| | | |
| n | def __call__(self, coro_func): | n | def __call__(self, coro): |
| idx = len(Loop._coroutines) | | index = len(Loop._registry) |
| Loop._coroutines.append(None) | | Loop._registry.append(None) |
| | | |
| n | async def wrapped(*args, **kwargs): | n | async def wrapper(*args, **kwargs): |
| while True: | | while True: |
| n | if Loop._stop_flag: | n | if Loop._finished: |
| return None | | return None |
| n | if Loop._step_idx != idx: | n | if Loop._current != index: |
| await asyncio.sleep(0) | | await asyncio.sleep(0) |
| continue | | continue |
| n | result = await coro_func(*args, **kwargs) | n | res = await coro(*args, **kwargs) |
| if result is None: | | if res is None: |
| Loop._stop_flag = True | | Loop._finished = True |
| Loop._step_idx = (Loop._step_idx + 1) % len(Loop._corout | | Loop._current = (Loop._current + 1) % len(Loop._registry |
| ines) | | ) |
| if result is None: | | if res is None: |
| return None | | return None |
| t | return wrapped | t | return wrapper |