为什么asyncio.sleep(延迟)比给定的延迟更早完成?

时间:2017-09-19 17:37:35

标签: python-asyncio

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())

输出结果为:

output

为什么等待asyncio.sleep(暂停)在0.093654s之后完成了????????????

1 个答案:

答案 0 :(得分:0)

它是Windows上asyncio的一个错误/功能。您可以阅读讨论here