asyncio标准库2 Hello Clock

it2022-05-05  210

asyncio标准库2 Hello Clock

如何调度协程,并发运行

asyncio.gather方法可以聚合协程or futuredef gather(*coros_or_futures, loop=None, return_exceptions=False)

import asyncio async def print_every_second(): "Print seconds" while True: for i in range(60): print(i, 's') await asyncio.sleep(1) async def print_every_minute(): for i in range(1, 10): await asyncio.sleep(60) print(i, 'minute') loop = asyncio.get_event_loop() loop.run_until_complete( asyncio.gather(print_every_second(), print_every_minute()) ) loop.close() posted on 2018-03-19 11:22 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/8600894.html


最新回复(0)