Aller au contenu

Blocking call in async (ARCH2028)

Ce contenu n’est pas encore disponible dans votre langue.

Key python.async.blocking-call-in-async · Severity error · Category async

Synchronous blocking calls (time.sleep, blocking IO, sync HTTP/DB drivers) inside an async function stall the event loop and starve all concurrency. Use the asynchronous equivalents instead.

import time
async def poll():
time.sleep(5) # blocks the event loop
return await fetch_status()
import asyncio
async def poll():
await asyncio.sleep(5) # does not block the loop
return await fetch_status()

Replace the blocking call with its async equivalent, or offload it to a thread pool via run_in_executor.