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