Leggi il codice attentamente
import asyncio
async def fetch(sem: asyncio.Semaphore, name: str) -> str:
async with sem:
# Stand-in for a real I/O call.
await asyncio.sleep(0.001)
return name
async def main() -> list[str]:
sem = asyncio.Semaphore(2)
names = ["a", "b", "c", "d", "e"]
results = await asyncio.gather(*[fetch(sem, n) for n in names])
return results
print(asyncio.run(main()))
Cosa stamperà il programma? Scrivi qui: