Using synchronous HTTP clients like urllib3
, requests
, or httpx.Client
in asynchronous code blocks the
entire event loop. This undermines the primary advantage of asynchronous programming - the ability to perform concurrent operations without blocking
execution.
When an async function makes a synchronous HTTP request:
- The event loop is completely blocked until the HTTP operation completes
- No other coroutines can run during this time, even if they’re ready to execute
- The responsiveness of the application is degraded
- In server applications, this can cause timeouts or failures for other concurrent requests
Instead, async-compatible HTTP clients should be used:
-
httpx.AsyncClient
- works with asyncio, Trio, and AnyIO
-
aiohttp.ClientSession
- works with asyncio
-
asks
- works with Trio and asyncio
Using these libraries allows other tasks to continue executing while waiting for HTTP responses, significantly improving application performance
and responsiveness.