Skip to content

blocking-http-call-in-async-function (ASYNC210)

Derived from the flake8-async linter.

What it does

Checks that async functions do not contain blocking HTTP calls.

Why is this bad?

Blocking an async function via a blocking HTTP call will block the entire event loop, preventing it from executing other tasks while waiting for the HTTP response, negating the benefits of asynchronous programming.

Instead of making a blocking HTTP call, use an asynchronous HTTP client library such as aiohttp or httpx.

Example

async def fetch():
    urllib.request.urlopen("https://example.com/foo/bar").read()

Use instead:

async def fetch():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://example.com/foo/bar") as resp:
            ...