Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 40 ยท Projects and Practice

asyncio Practice: Exercises with Solutions

Practise asyncio with six graded exercises: concurrent gathering, first-success racing, bounded map, timeouts with fallbacks, producer-consumer and retry decorators.

Reading about asyncio is not the same as writing it. These six exercises cover the skills from the whole course: concurrency, racing, limiting, timeouts, queues and decorators. Each has a starter editor with a function to complete and a small test harness that prints PASS or FAIL, followed by a solution you can compare with once you have tried.

How to use this page: edit the starter and press Run until it prints PASS. Only then open the matching solution. Resist the temptation to skip ahead; wrestling with the exercise is where the learning happens. The harnesses measure "time" in event-loop turns instead of seconds, so results are exact and quick.

Every exercise uses these two helpers, repeated in each editor so each one runs on its own:

  • work(name, turns) waits for a number of loop turns (standing in for network time) and returns the name.
  • turns_taken(coro) runs a coroutine and reports how many loop turns passed.

Exercise 1: gather in order

Write fetch_all(names, turns) that fetches every name concurrently using work and returns the results in the same order as names. The harness checks both the result and that it ran concurrently (fewer than 30 turns for four 10-turn jobs).

Python Exercise 1 - starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: asyncio.gather(*coroutines) preserves the order of its arguments.

Exercise 2: first success wins

Write first_success(factories): each item is a function returning a coroutine. Start all of them at once, return the result of the first one that succeeds, ignore the ones that raise, cancel the rest, and raise RuntimeError("all failed") if every one fails.

Python Exercise 2 - starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: create tasks, then loop over asyncio.as_completed(tasks); wrap each await in try/except Exception. Cancel what is left in a finally.

Exercise 3: bounded map

Write bounded_map(func, items, limit) that applies the async function func to every item with at most limit calls running at the same time, returning results in input order. The harness records the peak concurrency.

Python Exercise 3 - starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: a Semaphore and an inner coroutine that does async with sem: return await func(item).

Exercise 4: a timeout with a fallback

Write fetch_or_default(factory, seconds, default) that returns the coroutine's result, or default if it takes longer than seconds (use the real clock; the harness uses generous margins). A failure other than timeout must still propagate.

Python Exercise 4 - starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: try: async with asyncio.timeout(seconds): return await factory() and except TimeoutError. Remember where the TimeoutError is raised.

Exercise 5: producer and consumers

Write run_pipeline(numbers, consumers): one producer puts every number on a bounded queue (size 2) followed by one stop signal per consumer; consumers workers each take items, "process" them (square), and return their partial sum. The function returns the total of all squares.

Python Exercise 5 - starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: use None as the stop signal, await queue.put(...) in the producer so the bounded queue gives backpressure, and asyncio.gather over the producer and consumers.

Exercise 6: a retry decorator

Write a decorator retry(times) for async functions: call the function up to times times, retrying only on ConnectionError, and re-raise the last error if all attempts fail. Use functools.wraps.

Python Exercise 6 - starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: the decorator returns an async def wrapper(*args, **kwargs) with a for attempt in range(times) loop; the expected output is flaky -> ok after 3 calls, bad -> 1 call, error propagated, name kept: flaky.

Solutions

Compare your code with these once your harness passes (or if you are truly stuck). There are many valid answers; what matters is that the behaviour matches.

Solution 1

Python Solution 1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution 2

Python Solution 2

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution 3

Python Solution 3

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution 4

Python Solution 4

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

One subtlety: a TimeoutError raised by the function itself (say, a socket timeout) would also be turned into the default here. If that matters, test cm.expired() on the context manager object before returning the default.

Solution 5

Python Solution 5

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Solution 6

Python Solution 6

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Where to go from here

  • Rebuild the crawler, chat room or scheduler from memory, then add two of the suggested extensions.
  • Turn each exercise into a proper test using the techniques from testing async code.
  • Review common mistakes against code you wrote earlier in the course.
  • Port one of your own blocking scripts (downloads, file processing, API calls) to asyncio, and measure the difference.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which tool returns results in the same order as the awaitables you passed in?

    gather returns a list in argument order, while as_completed yields results in the order they finish.
  2. 2. In a bounded producer-consumer pipeline, what gives the producer backpressure?

    put() on a full bounded queue suspends the producer until a consumer takes an item.
  3. 3. In a first-success race, why cancel the remaining tasks afterwards?

    Losing tasks would otherwise keep running (and consuming connections) for a result nobody wants.
  4. 4. Why use functools.wraps in a decorator for async functions?

    wraps copies metadata such as name onto the wrapper, which helps logs, debugging and introspection.

Summary

  • Concurrency tools to remember: gather for ordered batches, as_completed for first-come results, Semaphore for limits, queues for pipelines, timeout for deadlines.
  • Always clean up what you start: cancel losers, gather them, and close what you open.
  • Writing small harnesses with turn counts or injected clocks makes async code easy to verify.
  • You now have the toolkit to design, test and debug real asyncio programs.

Next: Return to the Python asyncio course overview.