if __name__ == '__main__': loop = asyncio.get_event_loop() # taskqueue = asyncio.Queue(maxsize=100) loop.run_until_complete( asyncio.gather( crawler.proto_authors_source(loop), crawler.fetcher_task(), crawler.parser_task(), crawler.dbwriter_task(loop), crawler.timeout_control(loop) ) )
async def timeout_control(loop, timeout=20, check_period=10): logger.info("TIMEOUT CONTROL UP") while True: await asyncio.sleep(check_period) if taskq.empty() and parserq.empty() and dbwq.empty() and _source_end: # taskq и прочие - очереди проброшенные между корутинами, _source_end - флаг завершения корутины - источника данных logger.info("TIMEOUT CONTROL | waiting for completion...") await loop.shutdown_asyncgens() await asyncio.sleep(timeout) logger.info("TIMEOUT CONTROL | close event loop") loop.stop() break logger.info("TIMEOUT CONTROL DOWN")
Велосипед даже едет, но подпрыгивает на этом месте.
INFO:crawler:DBWRITER UP
INFO:crawler:TIMEOUT CONTROL UP
INFO:crawler:PARSER UP
INFO:crawler:FETCHER UP
INFO:crawler:SOURCE AuthorsList UP
INFO:crawler:SOURCE DOWN
INFO:crawler:TIMEOUT CONTROL | waiting for completion...
INFO:crawler:TIMEOUT CONTROL | close event loop
INFO:crawler:TIMEOUT CONTROL DOWN
Traceback (most recent call last):
File "D:/google-drive/Dropbox/samlibcrawler/run.py", line 21, in <module>
crawler.timeout_control(loop)
File "C:\Program Files\Python36\Lib\asyncio\base_events.py", line 465, in run_until_complete
raise RuntimeError('Event loop stopped before Future completed.')
RuntimeError: Event loop stopped before Future completed.
Все-таки, как верно завершать эвент луп?