Skip to main content

Python Async Pitfalls: Why async def Doesn't Make Your Code Fast

Adding async def to a function won't speed it up. Here are the asyncio mistakes that trip people up most, with timed examples of what actually helps.

Updated 7 min readMarkdown
Python Async Pitfalls: Why async def Doesn't Make Your Code Fast

A lot of people assume that turning functions into async def makes an app faster. Usually it doesn't, and sometimes it makes things slower.

Async doesn't speed up a single line of code. It lets one thread do other work while a task is waiting. If nothing is waiting, there's nothing to gain.

What async def actually gives you

  • Calling an async def function returns a coroutine object. The body doesn't run yet.

  • Coroutines run on an event loop: one thread, tasks taking turns.

  • A task only gives up its turn at an await that really suspends, like a network call, a sleep or a queue read.

The rule: async is efficient waiting, not faster computing.

Before reaching for it, ask what your code is actually slow at:

Mermaid Diagram

Note: All timings below come from a single-core Linux box running Python 3.12. Your numbers will differ, but the patterns won't.

Pitfall 1: Forgetting to await

import asyncio

async def fetch_user():
    await asyncio.sleep(0.1)
    return {"id": 1}

async def main():
    fetch_user()  # forgot await
    print("done")

asyncio.run(main())
main.py:8: RuntimeWarning: coroutine 'fetch_user' was never awaited
  fetch_user()  # forgot await
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
done

The call created a coroutine and threw it away. Nothing ran. Use user = await fetch_user(), or asyncio.create_task(fetch_user()) if you want it to run in the background.

Pitfall 2: Awaiting one thing at a time

This is the most common reason async code isn't faster. Each await finishes before the next line starts:

import asyncio, time

async def call(n):
    await asyncio.sleep(1)  # pretend this is a network request
    return n

async def sequential():
    return [await call(i) for i in range(3)]

async def concurrent():
    return await asyncio.gather(*(call(i) for i in range(3)))

for fn in (sequential, concurrent):
    start = time.perf_counter()
    asyncio.run(fn())
    print(f"{fn.__name__}: {time.perf_counter() - start:.2f}s")
sequential: 3.00s
concurrent: 1.00s

Three one-second waits took three seconds one after another and one second together. On Python 3.11+, asyncio.TaskGroup does the same job with better error handling (see Pitfall 6).

Pitfall 3: Blocking the event loop

time.sleep(), requests.get(), most sync database drivers and heavy file reads all block the entire loop. Every other task freezes until they return.

Here's a heartbeat task that ticks every half second, while a blocking call runs:

import asyncio, time

async def heartbeat():
    start = time.perf_counter()
    while True:
        print(f"tick {time.perf_counter() - start:.1f}s")
        await asyncio.sleep(0.5)

async def main():
    hb = asyncio.create_task(heartbeat())
    await asyncio.sleep(0.1)
    time.sleep(2)  # blocking call inside async code
    await asyncio.sleep(0.6)
    hb.cancel()

asyncio.run(main())
tick 0.0s
tick 2.1s
tick 2.6s

The heartbeat went silent for two seconds. In a web server, that's every other request hanging.

Fixes:

  • Use an async library (httpx, aiohttp, asyncpg) and await asyncio.sleep().

  • If a library has no async version, run it in a thread: await asyncio.to_thread(blocking_fn, arg) (Python 3.9+). I swapped time.sleep(2) for await asyncio.to_thread(time.sleep, 2) and the heartbeat ticked normally.

Finding the culprit: run your app in asyncio debug mode. It logs any step that holds the loop for more than 100 ms.

import asyncio, time

async def handler():
    time.sleep(0.5)  # blocking call hiding in async code

asyncio.run(handler(), debug=True)
Executing <Task finished name='Task-1' coro=<handler() done, ...> took 0.500 seconds

FastAPI note: async def endpoints run directly on the event loop, so a blocking call inside one freezes every request. Plain def endpoints are run in a thread pool instead. If you must call blocking code, either use def or wrap the call in to_thread.

Pitfall 4: CPU-bound work

Wrapping CPU work in async def changes nothing. There's no await that waits, so the function just runs start to finish and nothing else gets a turn.

import asyncio, time
from concurrent.futures import ProcessPoolExecutor

def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

async def fib_async(n):  # async, but still CPU-bound
    return fib(n)

async def main():
    loop = asyncio.get_running_loop()

    start = time.perf_counter()
    [fib(33) for _ in range(3)]
    print(f"plain loop:        {time.perf_counter() - start:.2f}s")

    start = time.perf_counter()
    await asyncio.gather(*(fib_async(33) for _ in range(3)))
    print(f"async def + gather: {time.perf_counter() - start:.2f}s")

    with ProcessPoolExecutor() as pool:
        start = time.perf_counter()
        await asyncio.gather(*(loop.run_in_executor(pool, fib, 33) for _ in range(3)))
        print(f"process pool:      {time.perf_counter() - start:.2f}s")

if __name__ == "__main__":
    asyncio.run(main())
plain loop:        1.20s
async def + gather: 1.22s
process pool:      1.14s

All three took about the same. The async version is no faster than the plain loop. And while it ran, a heartbeat task I added ticked once in about a second of work, so the loop was effectively frozen.

The process pool wasn't faster either, because my test machine has a single core. What it did was keep the loop responsive: the heartbeat kept ticking every 0.2 seconds. On a multi-core machine, the jobs also run in parallel.

Threads won't help for pure-Python CPU work in standard CPython builds, because the GIL lets only one thread run Python code at a time. Use processes, or move the hot path to native code (NumPy, Rust, C extensions).

Pitfall 5: Unlimited concurrency

gather on 10,000 coroutines starts 10,000 requests at once. That's a quick way to hit rate limits, exhaust connection pools or run out of file descriptors. Cap it with a semaphore:

import asyncio

in_flight = peak = 0

async def fetch(i, limit):
    global in_flight, peak
    async with limit:
        in_flight += 1
        peak = max(peak, in_flight)
        await asyncio.sleep(0.05)  # pretend API call
        in_flight -= 1

async def main():
    limit = asyncio.Semaphore(20)  # at most 20 calls in flight
    await asyncio.gather(*(fetch(i, limit) for i in range(1000)))
    print("peak in flight:", peak)

asyncio.run(main())
peak in flight: 20

Without the semaphore, the peak was 1,000.

Pitfall 6: Tasks that fail quietly or vanish

gather doesn't cancel the others when one fails. The exception reaches you, but the remaining tasks keep running in the background. TaskGroup (3.11+) cancels them:

import asyncio

async def boom():
    await asyncio.sleep(0.1)
    raise ValueError("boom")

async def slow(log):
    try:
        await asyncio.sleep(0.3)
        log.append("slow finished")
    except asyncio.CancelledError:
        log.append("slow cancelled")
        raise

async def main():
    for name in ("gather", "TaskGroup"):
        log = []
        try:
            if name == "gather":
                await asyncio.gather(boom(), slow(log))
            else:
                async with asyncio.TaskGroup() as tg:
                    tg.create_task(boom())
                    tg.create_task(slow(log))
        except* ValueError:
            pass
        await asyncio.sleep(0.4)
        print(f"{name:9s} -> {log}")

asyncio.run(main())
gather    -> ['slow finished']
TaskGroup -> ['slow cancelled']

Fire-and-forget tasks can disappear. The event loop only holds a weak reference to tasks, so a task nobody references can be garbage collected before it finishes. The asyncio docs recommend keeping your own reference:

background_tasks = set()

def fire_and_forget(coro):
    task = asyncio.create_task(coro)
    background_tasks.add(task)
    task.add_done_callback(background_tasks.discard)
    return task

Async isn't free

Awaiting a coroutine is cheap. Turning each one into a task is not:

Test Time
1,000,000 plain function calls about 0.07s
1,000,000 awaited coroutines about 0.10s
100,000 coroutines through gather about 0.66s

If you're making a handful of calls, or the work finishes instantly, plain sync code is simpler and often faster. Don't spawn a task per tiny operation.

Quick reference

Symptom Likely cause Fix
"coroutine was never awaited" Missing await Add await, or create_task
Async is as slow as sync Awaiting one at a time gather or TaskGroup
Everything freezes during one call Blocking call in async code Async library or to_thread
High CPU, no speedup CPU-bound work Process pool
Rate limits, "too many open files" Unbounded concurrency Semaphore
One failure, others keep running gather TaskGroup (3.11+)
Background task never finishes Task garbage collected Keep a reference

Wrap-up

Async is a tool for overlapping waits. Before you add async def for speed, check three things: is the code mostly waiting, are you awaiting things concurrently, and is anything inside blocking the loop? If the answer to the first is no, async won't help.


Published via ZyVOP — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium & Hashnode in 1 click.

About the author

  • One platform to write, cross-post to Dev.to, Medium, WordPress, Hashnode, and Bluesky, and protect your SEO with canonical links. Zero paywalls and full content ownership.

No comments yet. Be the first to comment.