如何调度协程,并发运行
asyncio.gather方法可以聚合协程or future
def 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()