Advanced
Run jobs with a concurrency limit
Write async def run_limited(factories, limit). factories is a list of functions that take no arguments and each return a coroutine (for example lambda: job(3); calling it gives the coroutine to await). Run them all together but with **at most limit running at once** (use asyncio.Semaphore), and return the results in the order of factories.
The program runs five jobs with limit=2 and should print [0, 2, 4, 6, 8].
What Check tests
- Five jobs with limit 2 return the results in order
- With limit 2, two jobs run at once and never more
- With limit 1, jobs run one at a time
- A limit bigger than the job count works
- No jobs give an empty list
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.