Python traps 2 · Chapter 15 of 16

One failure in asyncio.gather

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Libraries
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: one phone off, and nobody got the push

    Write reproduce() in repro.py. Make a list of three riders, "Santosh Kamble", "Imran Shaikh" and "Prakash More", in that order, and a fake push(rider), an async def, that raises ConnectionError("phone off") at once for Imran Shaikh and, for the others, awaits asyncio.sleep(0) (the network) and then records the rider. Return asyncio.run(notify_riders(riders, push)), with notify_riders from pushes.py.

    The check runs your reproduce() twice, each time in a fresh python3: beside the original pushes.py it must return {"sent": [], "failed": ["Santosh Kamble", "Imran Shaikh", "Prakash More"]} (the whole batch failed), and beside a fixed pushes.py it must return {"sent": ["Santosh Kamble", "Prakash More"], "failed": ["Imran Shaikh"]}. So your fake must really fail for Imran alone. No real waiting and no input(): asyncio.sleep(0) only hands the turn to the other pushes, and reproduce() must finish within 3 seconds.

  2. Task 2

    pushes.py: one phone off is one rider failed

    Fix notify_riders(riders, push) in pushes.py. It returns {"sent": [riders], "failed": [riders]}, with every rider in exactly one of the two lists, both in the order the riders came in: a rider whose await push(rider) returns is sent, a rider whose push raises (any exception) is failed, and a failure never fails, stops or cancels any other rider's push.

    Keep sending all the pushes at once, not one after another, and push each rider once. The check imports a fresh copy of your pushes.py and uses its own fake push, which fails at once for chosen riders (the first, the middle, the last, all of them, with different exceptions) and, for the others, awaits the network a few times before it records the push, as a real call does. It also holds the first push until the last one has started, at most 1 second. A TaskGroup cancels the pushes still on their way when one fails.

  3. Task 3

    test_pushes.py: a test that would have caught it

    Write a pytest file, test_pushes.py, that would have caught this bug before the lunch pushes. The check runs your file three times: beside the original pushes.py, where at least one test must fail; beside the fixed pushes.py, where every test must pass; and beside a wrong fix that sends the pushes in an asyncio.TaskGroup, so the first failure cancels the pushes still on their way, where at least one test must fail.

    So use a fake push that fails at once for one rider with others after it, and whose good pushes await asyncio.sleep(0) before they record the rider, as a real network call does; check the whole result, and that every other rider got the push once. Use plain def test functions that call asyncio.run themselves (pytest-asyncio is not installed). No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: one phone off, and nobody got the push
  • pushes.py: one phone off is one rider failed
  • test_pushes.py: a test that would have caught it

More in Libraries

Build "One failure in asyncio.gather" yourself

Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.