Форум сайта python.su
Есть такой пример:
from concurrent.futures import ThreadPoolExecutor import multiprocessing import time import asyncio class Config(): def __call__(self): while True: print ("config reload") time.sleep(3) class Run(object): def __init__(self, config_file): self.config_file = config_file self.tasks = [] self.pool = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) self.loop = asyncio.get_event_loop() async def run_tasks(self): for i in range(0,10): self.tasks += [self.loop.create_task(self.task(i))] self.loop.run_until_complete(self.save_tasks()) async def save_tasks(self): data = await asyncio.gather(*self.tasks) print ("save data from tasks") async def read_config_file_loop(self): await self.loop.run_in_executor(None, self.config_file) async def task(self,i): print ("run task %s" % i ) await asyncio.sleep(10) async def sleep(self): await asyncio.sleep(10) async def run_tasks_loop(self): while True: await self.run_tasks() await self.sleep() def __call__(self): try: asyncio.async(self.read_config_file_loop()) asyncio.async(self.run_tasks_loop()) self.loop.run_forever() except KeyboardInterrupt: pass finally: self.loop.close() config_file = Config() Run(config_file)()
File "/usr/lib64/python3.6/asyncio/base_events.py", line 414, in run_forever
raise RuntimeError('This event loop is already running')
Отредактировано Bdfy1 (Фев. 25, 2019 23:30:06)
Офлайн