Dimitry GroupWork 10648
Aleksey GroupWork 10543
f1import asynciof1import asyncio
22
3class Loop:3class Loop:
n4    _funcs = []n4    funcs = []
5    _steps = []5    wrappers = []
6    _evt = None6    ready = None
7    _launched = False7    started = False
8    _halt = False8    stop = False
9    _count = 09    total = 0
1010
n11    def __call__(self, fn):n11    def __call__(self, func):
12        Loop._funcs.append(fn)12        Loop.funcs.append(func)
13        Loop._count = len(Loop._funcs)13        Loop.total = len(Loop.funcs)
1414
n15        async def outer(*a, **kw):n15        async def wrapper(*args, **kwargs):
16            if Loop._evt is None:16            if Loop.ready is None:
17                Loop._evt = asyncio.Event()17                Loop.ready = asyncio.Event()
1818
n19            async def runner():n19            async def step():
20                while True:20                while True:
n21                    if Loop._halt:n21                    if Loop.stop:
22                        return None22                        return None
n23                    out = await fn(*a, **kw)n23                    r = await func(*args, **kwargs)
24                    if out is None:24                    if r is None:
25                        Loop._halt = True25                        Loop.stop = True
26                        return None26                        return None
n27                    return outn27                    return r
28            Loop._steps.append(runner)28            Loop.wrappers.append(step)
29            if len(Loop._steps) == Loop._count:29            if len(Loop.wrappers) == Loop.total:
30                Loop._evt.set()30                Loop.ready.set()
31            await Loop._evt.wait()31            await Loop.ready.wait()
32            if not Loop._launched:32            if not Loop.started:
33                Loop._launched = True33                Loop.started = True
34                await Loop._cycle()34                await Loop._run()
35            return None35            return None
n36        return outern36        return wrapper
3737
38    @staticmethod38    @staticmethod
n39    async def _cycle():n39    async def _run():
40        seq = Loop._steps40        steps = Loop.wrappers
41        while True:41        while True:
t42            for r in seq:t42            for s in steps:
43                val = await r()43                r = await s()
44                if val is None:44                if r is None:
45                    return45                    return
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op