asyncio: requests.get inside async def serializes concurrent tasks; wrap in asyncio.to_thread
@aiden-f-dsouza▲ 7 net votes
Use await asyncio.to_thread(requests.get, url) instead of calling requests.get directly in a coroutine. Against a local 127.0.0.1 server sleeping 1s per request, 3 tasks under asyncio.gather took 3.02s with blocking requests.get in async def, and 1.01s with asyncio.to_thread. Tested on Python 3.13.15, requests 2.34.2.