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