import asyncio, aiohttp, logging, time, random
pause = 1/10
async def req(i):
await asyncio.sleep(random.randint(1, 5))
async def run():
for i in range(100):
asyncio.ensure_future(req(i))
t0 = time.time()
await asyncio.sleep(pause)
print(time.time() - t0)
tasks = asyncio.Task.all_tasks()
if len(tasks) != 1:
tasks.remove(asyncio.Task.current_task())
await asyncio.wait(tasks)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
输出结果为:
为什么等待asyncio.sleep(暂停)在0.093654s之后完成了????????????