An async function makes a call that blocks the event loop
What it finds
time.sleep(1) in an async def: a call that blocks holds up the whole event loop, so every other task waits until it returns. The async counterparts, such as asyncio.sleep, asyncio.create_subprocess_exec or an async HTTP client, let other tasks run meanwhile; work that has to block can go to asyncio.to_thread.
Reported are time.sleep, the functions of subprocess, os.system, os.popen, os.wait, the request functions of requests and urllib.request.urlopen, and input, called directly in the body of an async def, not in a function nested in it.
From the rule's source, blocking_call_in_async.rs.
import subprocessimport time import requests async def work(url): time.sleep(1)tx0 reports: time.sleep blocks the event loop, and every other task waits for it subprocess.run(["ls"])tx0 reports: subprocess.run blocks the event loop requests.get(url, timeout=5)tx0 reports: requests.get blocks the event loop def inner(): time.sleep(1) return inner def sync(): time.sleep(1) Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:blocking-call-in-async" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:blocking-call-in-async] reason="..."tx0 explain tx0/python:blocking-call-in-async shows the same in the terminal. Found a false positive? Report it.