Blocking call in async code
This is how the comment reads inline, under the flagged line.
Why it matters
A blocking call inside async def freezes the event loop: every other request waits for this one.
What to do
Use the async client (httpx.AsyncClient, asyncio.sleep, aiofiles) or await asyncio.to_thread(...).
Examples
async def fetch(u):
return requests.get(u, timeout=5)async def fetch(u):
async with httpx.AsyncClient() as c:
return await c.get(u, timeout=5)What it catches
It reads the shape of the code: functions, blocks, loops and calls. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
import time
async def f():
time.sleep(1)
Left alone
import asyncio
async def f():
await asyncio.sleep(1)
Research
Blocking file, process or HTTP calls in server or async code. One blocking call stalls every other request on the event loop.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"blocking-in-async"
],
"severity": {
"blocking-in-async": "low"
}
}
}
time.sleepblocks the event loop insideasync def f. Every other task waits.