Menu

asyncio.gather() Still Takes 2.5 Seconds? Find the Blocking Call with Debug Mode
📰

asyncio.gather() Still Takes 2.5 Seconds? Find the Blocking Call with Debug Mode

DEV Community: fastapi·Blogs World·18 days ago
#39FVxNBw
#devtofastapi#devto#dev#asyncio#async
Reading 0:00
15s threshold

You wrapped five calls in asyncio.gather() , expecting them to finish in about the time of the slowest one. Instead the total is the sum of all five, exactly as if you had written a plain for loop.

gather() is almost never the problem. Something inside one of your coroutines is blocking the event loop : a call that does real waiting without ever handing control back with await . While it runs, no other task can make progress, so your "concurrent" code quietly turns sequential.

Here is how to prove it in a minute, find the exact culprit, and fix it.

The 10-line reproduction

import asyncio , time

async def fetch_blocking ( i ): time . sleep ( 0.5 ) # looks harmless, freezes the whole loop return i

async def fetch_ok ( i ): await asyncio . to_thread ( time . sleep , 0.5 ) # waits in a worker thread return i

async def main (): t = time . perf_counter () await asyncio . gather ( * ( fetch_blocking ( i ) for i in range ( 5 ))) print ( f " blocking: { time . perf_counter () - t : . 2 f } s " )

t = time .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.