Blocking Call in Async Context

Event loop is blocked by synchronous code

Quick Answer

You are calling a blocking function inside async code, which freezes the event loop. Use asyncio.to_thread() or async libraries instead.

Why This Happens

The asyncio event loop runs in a single thread. Calling blocking functions like time.sleep(), requests.get(), or CPU-intensive code blocks the entire loop.

The Problem

import asyncio, time, requests

async def fetch():
    response = requests.get('https://api.example.com')  # Blocks!
    time.sleep(1)  # Blocks!
    return response.json()

The Fix

import asyncio, httpx

async def fetch():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://api.example.com')
    await asyncio.sleep(1)
    return response.json()

# Or wrap sync code:
async def fetch():
    response = await asyncio.to_thread(requests.get, 'https://api.example.com')
    return response.json()

Step-by-Step Fix

  1. 1

    Use async libraries

    Replace requests with httpx/aiohttp. Replace time.sleep with asyncio.sleep.

  2. 2

    Use asyncio.to_thread()

    Wrap blocking calls: await asyncio.to_thread(func, args).

  3. 3

    Use run_in_executor()

    For Python <3.9: await loop.run_in_executor(None, func).

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free