What is the difference between parallel and async in Python?

Mar 24, 2024 ยท 2 min read

When writing Python code that needs to perform multiple tasks at the same time, you have two main options: parallel and asynchronous programming. Both approaches allow your program to execute more than one thing simultaneously, but they work in fundamentally different ways.

Parallel Execution

Parallel programming in Python allows a program to run multiple parts of its code in parallel, leveraging multiple CPU cores on the machine. This is achieved through modules like multiprocessing and threading.

Here is a simple parallel program:

import multiprocessing

def print_square(num):
    print(num**2)

if __name__ == "__main__":
    p1 = multiprocessing.Process(target=print_square, args=(2,)) 
    p2 = multiprocessing.Process(target=print_square, args=(3,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

This starts up two new Python processes, each running the print_square function with different arguments. They will print 4 and 9 simultaneously, utilizing two CPU cores.

The key thing is that true parallel execution requires multiple Python interpreters running your code.

Asynchronous Programming

Async code runs in a single thread, but allows long-running functions to yield control back so other work can happen while waiting. This uses an event loop and callbacks. The asyncio module is commonly used for this.

Here is async code that simulates two long tasks:

import asyncio

async def print_square(num):
    print(f"Calculating {num**2}")
    await asyncio.sleep(1) 
    print(f"{num**2} printed")

async def main():
    await asyncio.gather(
        print_square(2),
        print_square(3)
    )

asyncio.run(main())

This will print out both squares sequentially, but simulate 1 second of other work happening in between.

So async allows logical concurrency without true parallelism. It works great for IO-bound workloads.

The choice between parallel and async depends on your specific use case and resources available. Both are key tools for concurrent Python programming.

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: