Хайруллина Алина GroupWork 11002
action22k GroupWork 10465
f1import asynciof1import asyncio
22
3class Loop:3class Loop:
n4    _tasks = []n4    _registry = []
5    _pointer = 05    _current = 0
6    _halted = False6    _finished = False
77
8    def __init__(self):8    def __init__(self):
9        pass9        pass
1010
n11    def __call__(self, routine):n11    def __call__(self, coro):
12        slot = len(Loop._tasks)12        index = len(Loop._registry)
13        Loop._tasks.append(None)13        Loop._registry.append(None)
1414
n15        async def envelope(*a, **kw):n15        async def wrapper(*args, **kwargs):
16            while True:16            while True:
n17                if Loop._halted:n17                if Loop._finished:
18                    return None18                    return None
n19                if Loop._pointer != slot:n19                if Loop._current != index:
20                    await asyncio.sleep(0)20                    await asyncio.sleep(0)
21                    continue21                    continue
n22                outcome = await routine(*a, **kw)n22                res = await coro(*args, **kwargs)
23                if outcome is None:23                if res is None:
24                    Loop._halted = True24                    Loop._finished = True
25                Loop._pointer = (Loop._pointer + 1) % len(Loop._tasks)25                Loop._current = (Loop._current + 1) % len(Loop._registry
 >)
26                if outcome is None:26                if res is None:
27                    return None27                    return None
t28        return envelopet28        return wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op