Python traps 2 · Chapter 16 of 16
A race without threads
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.
- Task 1
repro.py: a double tap, a single-use coupon, two charges
Write reproduce() in repro.py. Make CouponBook({"FIRSTDABBA": 49}) from coupons.py and a fake charge(customer, amount), an async def, that awaits asyncio.sleep(0) (the network) and then records the charge. Run two redemptions of FIRSTDABBA by customer "C066" at once, the double tap, with asyncio.gather inside asyncio.run, and return how many charges your fake recorded, as an int.
The check runs your reproduce() twice, each time in a fresh python3: beside the original coupons.py it must return 2 (both taps charged), and beside a fixed coupons.py it must return 1. So the two redemptions must run together, and the fake must await before it records. No real waiting and no input(): asyncio.sleep(0) only hands the turn to the other redemption, and reproduce() must finish within 3 seconds.
- Task 2
coupons.py: one coupon, one charge
Fix CouponBook.redeem(code, customer, charge) in coupons.py so that, however many redemptions run at once on the event loop: an unknown or used code returns False and charges nothing; a code not used yet awaits charge(customer, price) once, marks the code used and returns True; and when the charge raises, the error comes out of redeem and the code stays unused, so a retry works.
Keep CouponBook(codes) ({code: offer price}), codes, used and redeem as an async def with the same arguments. The check imports a fresh copy of your coupons.py and uses its own fake charge, which awaits the network a few times before it records the charge, as a real call does. It runs redemptions together with asyncio.gather: two and four taps of one code, two codes at once, and a charge that is declined and then retried. Anything that blocks the event loop, such as a threading.Lock held across an await, fails the check.
- Task 3
test_coupons.py: a test that would have caught it
Write a pytest file, test_coupons.py, that would have caught the double charge before it reached customers. The check runs your file three times: beside the original coupons.py, where at least one test must fail; beside the fixed coupons.py, where every test must pass; and beside a wrong fix that marks the code used before the charge and never gives it back, where at least one test must fail.
So test both sides: two redemptions of one code at once, with a fake charge that awaits asyncio.sleep(0) before it records, give one True and one charge; and a charge that fails leaves the code unused, so a retry with a working charge gets True. Use plain def test functions that call asyncio.run themselves (pytest-asyncio is not installed), and pytest.raises for the failed charge. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: a double tap, a single-use coupon, two charges
- coupons.py: one coupon, one charge
- test_coupons.py: a test that would have caught it
More in Libraries
Build "A race without threads" 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.