[Python] Using a semaphore with asyncio in Python

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to limit the number of simultaneous async functions running using a semaphore, but I cannot get it to work. My code boils down to this:

    import asyncio


    async def send(i):

    print(f"starting {i}")
    await asyncio.sleep(4)
    print(f"ending {i}")


    async def helper():
    async with asyncio.Semaphore(value=5):
    await asyncio.gather(*[
    send(1),
    send(2),
    send(3),
    send(4),
    send(5),
    send(6),
    send(7),
    send(8),
    send(9),
    send(10),
    ])


    if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(helper())
    loop.close()


    The output is:

    starting 1
    starting 2
    starting 3
    starting 4
    starting 5
    starting 6
    starting 7
    starting 8
    starting 9
    starting 10
    ending 1
    ending 2
    ending 3
    ending 4
    ending 5
    ending 6
    ending 7
    ending 8
    ending 9
    ending 10


    I hope and expect that only 5 will run at time, however all 10 start and stop at the same time. What am I doing wrong?

    Continue reading...

Compartilhe esta Página