Fix the blocking call audit for an async handler
Your task
An AI built a FastAPI async def upload handler that reads, hashes and stores avatars. Throughput is low and the event loop lags, because sync calls inside an async handler block the one thread that serves every request. Your team audits each call in such a handler and labels what it needs.
An assistant wrote classify_async_calls(calls). It is in the editor. It has a bug. Find it and fix it.
Input and output:
calls: the calls in the handler, in order, as written, such astime.sleeporawait file.read.- Return one verdict per call, in the same order:
"ok","missing_await","use_await"or"use_threadpool".
Rules the tests check:
- Trim whitespace from both ends of each call first. Names then match exactly, case included.
- The verdict is the first of these that applies:
"ok"when the call starts withawait(with the space). It is already awaited."missing_await"when the call is one ofasyncio.sleep,file.read,run_in_threadpoolanddb.fetch. These are coroutines, which do nothing unless awaited."use_await"when the call is one oftime.sleep,file.file.read,requests.getandrequests.post. Each has an async version to await instead."use_threadpool"when the call is one ofhashlib.sha256,hashlib.md5,open,f.write,bcrypt.hashpwandPIL.Image.open. These have no async version, so they belong on a worker thread."ok"for anything else, including an empty entry.
Examples
classify_async_calls(calls: list[str]) -> list[str]Example 1
Input
classify_async_calls(["file.file.read", "hashlib.sha256", "time.sleep", "open", "f.write"])Output
["use_await", "use_threadpool", "use_await", "use_threadpool", "use_threadpool"]Example 2
Input
classify_async_calls(["await file.read", "await asyncio.sleep", "return digest"])Output
["ok", "ok", "ok"]Example 3
Input
classify_async_calls([])Output
[]