fr
fr

Question from the Python test

Waiting for validation

In an asynchronous function, what is the role of a Semaphore?

import asyncio


async def request(index, sem):
    async with sem:
        print(f'Hello from request {index}!')
        await asyncio.sleep(3)


async def main():
    sem = asyncio.Semaphore(3)
    async with asyncio.TaskGroup() as tg:
        for index in range(20):
            tg.create_task(request(index, sem))


if __name__ == "__main__":
    asyncio.run(main())
Author: John PendenqueStatus: Waiting for validationQuestion not yet passed
0
Community EvaluationsNo one has reviewed this question yet, be the first!