Command Palette

Search for a command to run...

Hectal
Phase 12: Concurrency and Async
Python · Exercise 4 of 6 PreviousNext
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

  1. Five jobs with limit 2 return the results in order
  2. With limit 2, two jobs run at once and never more
  3. With limit 1, jobs run one at a time
  4. A limit bigger than the job count works
  5. 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.