Millennium · Concurrency
Debug missing output in Python async HTTP flow
TrueInterview
October 7, 2026 · 1 min read
A Python script is intended to call two HTTP endpoints in order, decode the JSON response from each, and print a combined total. The script is shown below, but when it is run it exits without printing the expected output.
Assume an async HTTP client such as aiohttp is available. For example, if the first endpoint returns {"value": 4} and the second returns {"value": 9}, the desired behavior is to print total: 13; the actual behavior is no output.
import asyncio
import aiohttp
async def get_json(session, url):
async with session.get(url) as response:
return await response.json()
async def compute_total():
async with aiohttp.ClientSession() as session:
first = get_json(session, "https://api.example.net/alpha")
second = get_json(session, "https://api.example.net/beta")
total = (await first)["value"] + (await second)["value"]
print("total:", total)
def run():
compute_total()
if __name__ == "__main__":
run()
Tasks
- Explain why the script prints nothing or may behave inconsistently.
- Show the changes needed so the HTTP requests are actually executed and the
printstatement runs reliably. - Follow-up: if the requirement is changed so that both HTTP calls must run concurrently, show the idiomatic implementation and discuss pitfalls such as cancellation, exception handling, and program exit.
Constraints:
- Both endpoints return a JSON object containing a numeric
"value"field. - The solution should use an async HTTP client such as
aiohttp.
Loading comments…