Лукьянов Артём Васильевич, 317, Севастопольский филилал GroupWork 11041
Сагура Николай, 392, сев. филиал GroupWork 10435
f1import asynciof1import asyncio
22
3class Loop:3class Loop:
n4    _registry_count = 0n4    _count = 0
5    _registry_funcs = []5    _funcs = []
6    _registry_args = []6    _call_args = []
7    _registry_started = 07    _started = 0
8    _all_started_event = None8    _all_started_event = None
n9    _finished_future = Nonen9    _done_fut = None
10    _scheduler_active = False10    _scheduler_running = False
1111
12    def __init__(self):12    def __init__(self):
13        pass13        pass
1414
n15    def __call__(self, coro_func):n15    def __call__(self, func):
16        cls = self.__class__16        cls = self.__class__
n17        my_idx = cls._registry_countn17        idx = cls._count
18        cls._registry_count += 118        cls._count += 1
19        cls._registry_funcs.append(coro_func)19        cls._funcs.append(func)
20        if len(cls._registry_args) < cls._registry_count:20        if len(cls._call_args) < cls._count:
21            cls._registry_args.append(None)21            cls._call_args.append(None)
2222
23        async def wrapper(*args, **kwargs):23        async def wrapper(*args, **kwargs):
nn24            cls = self.__class__
24            loop = asyncio.get_running_loop()25            loop = asyncio.get_running_loop()
n25            if cls._finished_future is None:n26            if cls._done_fut is None:
26                cls._finished_future = loop.create_future()27                cls._done_fut = loop.create_future()
27            if cls._all_started_event is None:28            if cls._all_started_event is None:
28                cls._all_started_event = asyncio.Event()29                cls._all_started_event = asyncio.Event()
n29            cls._registry_args[my_idx] = (args, kwargs)n30            cls._call_args[idx] = (args, kwargs)
30            cls._registry_started += 131            cls._started += 1
31            if cls._scheduler_active:32            if cls._scheduler_running:
32                if cls._registry_started == cls._registry_count:33                if cls._started == cls._count:
33                    cls._all_started_event.set()34                    cls._all_started_event.set()
n34                await cls._finished_futuren35                await cls._done_fut
35                return None36                return None
n36            cls._scheduler_active = Truen37            cls._scheduler_running = True
37            try:38            try:
n38                if cls._registry_started < cls._registry_count:n39                if cls._started < cls._count:
39                    await cls._all_started_event.wait()40                    await cls._all_started_event.wait()
40                else:41                else:
41                    cls._all_started_event.set()42                    cls._all_started_event.set()
n42                idx_cycle = 0n43                idx_local = 0
43                while True:44                while True:
n44                    func = cls._registry_funcs[idx_cycle]n45                    func_local = cls._funcs[idx_local]
45                    args_cycle, kwargs_cycle = cls._registry_args[idx_cy46                    args_local, kwargs_local = cls._call_args[idx_local]
>cle] 
46                    result = await func(*args_cycle, **kwargs_cycle)47                    res = await func_local(*args_local, **kwargs_local)
47                    if result is None:48                    if res is None:
48                        break49                        break
n49                    idx_cycle = (idx_cycle + 1) % cls._registry_countn50                    idx_local = (idx_local + 1) % cls._count
50                if not cls._finished_future.done():51                if not cls._done_fut.done():
51                    cls._finished_future.set_result(None)52                    cls._done_fut.set_result(None)
52            finally:53            finally:
n53                cls._scheduler_active = Falsen54                cls._scheduler_running = False
54                cls._registry_count = 055                cls._count = 0
55                cls._registry_funcs = []56                cls._funcs = []
56                cls._registry_args = []57                cls._call_args = []
57                cls._registry_started = 058                cls._started = 0
58                cls._all_started_event = None59                cls._all_started_event = None
t59                cls._finished_future = Nonet60                cls._done_fut = None
60            return None61            return None
61        return wrapper62        return wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op