Process items with exactly workers concurrent asyncio worker tasks reading from an asyncio.Queue.
For each item call the provided await process(item):
TransientError retry the item (up to max_attempts attempts in total).Return {"results": [...], "errors": {...}} where results[i] is the result for items[i] (or None
if it failed) and errors maps the string index to the exception type name. Shut the workers down
cleanly (no pending tasks). The harness checks the concurrency you achieved.
Implement run_pipeline. Input: items: list[str], workers: int, max_attempts: int. Returns: dict.
flaky:3 fails twice and succeeds on attempt 3; flaky:5 runs out of attempts.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
import asyncio
class TransientError(Exception):
pass
_state = {"active": 0, "peak": 0, "attempts": {}}
async def process(item):
_state["active"] += 1
_state["peak"] = max(_state["peak"], _state["active"])
try:
n = _state["attempts"][item] = _state["attempts"].get(item, 0) + 1
await asyncio.sleep(0.002)
kind, _, rest = item.partition(":")
if kind == "flaky" and n < int(rest.split(":")[0]):
raise TransientError(item)
if kind == "bad":
raise ValueError(item)
return item.upper()
finally:
_state["active"] -= 1
async def __harness__(fn, items, workers, max_attempts):
_state.update(active=0, peak=0, attempts={})
out = await fn(items, workers, max_attempts)
leftover = [t for t in asyncio.all_tasks() if t is not asyncio.current_task() and not t.done()]
return {**out, "peak_ok": _state["peak"] == min(workers, len(set(items))) or not items, "clean": not leftover}
import asyncio
async def run_pipeline(items: list[str], workers: int, max_attempts: int) -> dict:
results = [await process(i) for i in items]
return {"results": results, "errors": {}}
Run executes the public tests. Submit grades against public and hidden tests.
Process items with exactly workers concurrent asyncio worker tasks reading from an asyncio.Queue.
For each item call the provided await process(item):
TransientError retry the item (up to max_attempts attempts in total).Return {"results": [...], "errors": {...}} where results[i] is the result for items[i] (or None
if it failed) and errors maps the string index to the exception type name. Shut the workers down
cleanly (no pending tasks). The harness checks the concurrency you achieved.
Implement run_pipeline. Input: items: list[str], workers: int, max_attempts: int. Returns: dict.
flaky:3 fails twice and succeeds on attempt 3; flaky:5 runs out of attempts.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
import asyncio
class TransientError(Exception):
pass
_state = {"active": 0, "peak": 0, "attempts": {}}
async def process(item):
_state["active"] += 1
_state["peak"] = max(_state["peak"], _state["active"])
try:
n = _state["attempts"][item] = _state["attempts"].get(item, 0) + 1
await asyncio.sleep(0.002)
kind, _, rest = item.partition(":")
if kind == "flaky" and n < int(rest.split(":")[0]):
raise TransientError(item)
if kind == "bad":
raise ValueError(item)
return item.upper()
finally:
_state["active"] -= 1
async def __harness__(fn, items, workers, max_attempts):
_state.update(active=0, peak=0, attempts={})
out = await fn(items, workers, max_attempts)
leftover = [t for t in asyncio.all_tasks() if t is not asyncio.current_task() and not t.done()]
return {**out, "peak_ok": _state["peak"] == min(workers, len(set(items))) or not items, "clean": not leftover}
import asyncio
async def run_pipeline(items: list[str], workers: int, max_attempts: int) -> dict:
results = [await process(i) for i in items]
return {"results": results, "errors": {}}
Run executes the public tests. Submit grades against public and hidden tests.