When Async Python Outperforms Sync

Mar 17, 2024 ยท 2 min read

Async programming in Python allows code to execute out of order while waiting on long-running tasks like network I/O. This enables other work to interleave during the waits, improving utilization.

However, async doesn't automatically make things faster. It enables concurrency, but extra overhead is involved and async can actually be slower for CPU-bound work. Let's explore when async helps.

I/O-Bound Workloads See Speedup

Async shines when I/O is the bottleneck rather than CPU:

import asyncio

async def fetch(url):
    await asyncio.sleep(2) # simulate I/O wait
    return f"{url} response"

async def main():
    print(await fetch("url1"))
    print(await fetch("url2"))

asyncio.run(main())

Here async allows both fetches to be in flight simultaneously, cutting the total time nearly in half.

But for a CPU-heavy workload like:

def cpu_op():
   for i in range(10_000_000):
       pass
       
async def main():
   await cpu_op()
   await cpu_op()  

There is little benefit from async since the CPU is already maxed out. Async helps when I/O wait time dominates over compute time.

Overhead Costs Can Hurt

The async infrastructure introduces small overheads around scheduling and switching tasks. This can make async slower for workloads with tiny I/O waits or compute steps.

So while async enables concurrency, it still comes down to profiling and measurements to know precisely when it will speed up your specific application.

Key Takeaways

  • Async allows concurrency during I/O waits
  • But extra overhead is involved
  • Async speeds up I/O-bound workloads
  • But can be slower for heavy CPU processing
  • Always profile before and after to validate
  • The concurrency of async is a powerful tool - when applied properly to I/O-bound use cases. Measure first, don't assume async will universally speed up Python code.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: