Сагура Николай, 392, сев. филиал GroupWork 10435
Лукьянов Артём Васильевич, 317, Севастопольский филилал GroupWork 11041
f1import asynciof1import asyncio
22
3class Loop:3class Loop:
n4    _count = 0n4    _registry_count = 0
5    _funcs = []5    _registry_funcs = []
6    _call_args = []6    _registry_args = []
7    _started = 07    _registry_started = 0
8    _all_started_event = None8    _all_started_event = None
n9    _done_fut = Nonen9    _finished_future = None
10    _scheduler_running = False10    _scheduler_active = False
1111
12    def __init__(self):12    def __init__(self):
13        pass13        pass
1414
n15    def __call__(self, func):n15    def __call__(self, coro_func):
16        cls = self.__class__16        cls = self.__class__
n17        idx = cls._countn17        my_idx = cls._registry_count
18        cls._count += 118        cls._registry_count += 1
19        cls._funcs.append(func)19        cls._registry_funcs.append(coro_func)
20        if len(cls._call_args) < cls._count:20        if len(cls._registry_args) < cls._registry_count:
21            cls._call_args.append(None)21            cls._registry_args.append(None)
2222
23        async def wrapper(*args, **kwargs):23        async def wrapper(*args, **kwargs):
n24            cls = self.__class__n
25            loop = asyncio.get_running_loop()24            loop = asyncio.get_running_loop()
n26            if cls._done_fut is None:n25            if cls._finished_future is None:
27                cls._done_fut = loop.create_future()26                cls._finished_future = loop.create_future()
28            if cls._all_started_event is None:27            if cls._all_started_event is None:
29                cls._all_started_event = asyncio.Event()28                cls._all_started_event = asyncio.Event()
n30            cls._call_args[idx] = (args, kwargs)n29            cls._registry_args[my_idx] = (args, kwargs)
31            cls._started += 130            cls._registry_started += 1
32            if cls._scheduler_running:31            if cls._scheduler_active:
33                if cls._started == cls._count:32                if cls._registry_started == cls._registry_count:
34                    cls._all_started_event.set()33                    cls._all_started_event.set()
n35                await cls._done_futn34                await cls._finished_future
36                return None35                return None
n37            cls._scheduler_running = Truen36            cls._scheduler_active = True
38            try:37            try:
n39                if cls._started < cls._count:n38                if cls._registry_started < cls._registry_count:
40                    await cls._all_started_event.wait()39                    await cls._all_started_event.wait()
41                else:40                else:
42                    cls._all_started_event.set()41                    cls._all_started_event.set()
n43                idx_local = 0n42                idx_cycle = 0
44                while True:43                while True:
n45                    func_local = cls._funcs[idx_local]n44                    func = cls._registry_funcs[idx_cycle]
46                    args_local, kwargs_local = cls._call_args[idx_local]45                    args_cycle, kwargs_cycle = cls._registry_args[idx_cy
 >cle]
47                    res = await func_local(*args_local, **kwargs_local)46                    result = await func(*args_cycle, **kwargs_cycle)
48                    if res is None:47                    if result is None:
49                        break48                        break
n50                    idx_local = (idx_local + 1) % cls._countn49                    idx_cycle = (idx_cycle + 1) % cls._registry_count
51                if not cls._done_fut.done():50                if not cls._finished_future.done():
52                    cls._done_fut.set_result(None)51                    cls._finished_future.set_result(None)
53            finally:52            finally:
n54                cls._scheduler_running = Falsen53                cls._scheduler_active = False
55                cls._count = 054                cls._registry_count = 0
56                cls._funcs = []55                cls._registry_funcs = []
57                cls._call_args = []56                cls._registry_args = []
58                cls._started = 057                cls._registry_started = 0
59                cls._all_started_event = None58                cls._all_started_event = None
t60                cls._done_fut = Nonet59                cls._finished_future = None
61            return None60            return None
62        return wrapper61        return wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op