action22k GroupWork 10465
Долгих Данила GroupWork 10625
f1import asynciof1import asyncio
22
3class Loop:3class Loop:
n4    _registry = []n4    _coroutines = []
5    _current = 05    _step_idx = 0
6    _finished = False6    _stop_flag = False
77
8    def __init__(self):8    def __init__(self):
9        pass9        pass
1010
n11    def __call__(self, coro):n11    def __call__(self, coro_func):
12        index = len(Loop._registry)12        idx = len(Loop._coroutines)
13        Loop._registry.append(None)13        Loop._coroutines.append(None)
1414
n15        async def wrapper(*args, **kwargs):n15        async def wrapped(*args, **kwargs):
16            while True:16            while True:
n17                if Loop._finished:n17                if Loop._stop_flag:
18                    return None18                    return None
n19                if Loop._current != index:n19                if Loop._step_idx != idx:
20                    await asyncio.sleep(0)20                    await asyncio.sleep(0)
21                    continue21                    continue
n22                res = await coro(*args, **kwargs)n22                result = await coro_func(*args, **kwargs)
23                if res is None:23                if result is None:
24                    Loop._finished = True24                    Loop._stop_flag = True
25                Loop._current = (Loop._current + 1) % len(Loop._registry25                Loop._step_idx = (Loop._step_idx + 1) % len(Loop._corout
>)>ines)
26                if res is None:26                if result is None:
27                    return None27                    return None
t28        return wrappert28        return wrapped
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op