Concurrency vs. Parallelism in Python

A comparison of concurrency and parallelism, with Python examples using threading, multiprocessing, and asyncio.

On this page

Concurrency vs. Parallelism in Python

An interview question got me thinking about how concurrency and parallelism differ, and when each one helps.

ByteByteGo's video explains the distinction well. This screenshot is from the video.

ByteByteGo's comparison of concurrency and parallelism

  • Concurrency means making progress on multiple tasks over the same period. A single core can do this by interleaving their execution.
  • Parallelism means executing multiple tasks at the same instant, for example on separate CPU cores.

Imagine a chef alternating between stirring soup and frying eggs. That's concurrency. With two chefs cooking different dishes at the same time, you have parallelism.

What kind of work are you doing?

  • I/O-bound work spends most of its time waiting on disk, network, or database operations. The CPU can do other work while those operations finish.
  • CPU-bound work spends most of its time computing. Examples include video encoding, encryption, and scientific calculations.

NOTE

Real workloads often mix both. Look at which kind of work takes up most of the time before choosing a strategy.

Python tools and when to use them

ToolBest suited toRuns on multiple cores?Can run Python work in parallel?Notes
threadingI/O-bound workNot for simultaneous Python bytecode in standard CPython builds with the GILLimited by the GILCan overlap I/O waits
multiprocessingCPU-bound workYesYesEach process has its own memory

Examples

I/O-bound work with threads

import threading
import time

def download_file(name):
    print(f"{name} ้–‹ๅง‹ไธ‹่ผ‰...")
    time.sleep(2)  # ๆจกๆ“ฌ็ญ‰ๅพ…ไธ‹่ผ‰
    print(f"{name} ไธ‹่ผ‰ๅฎŒๆˆ")

start = time.time()

t1 = threading.Thread(target=download_file, args=("ๆช”ๆกˆ1",))
t2 = threading.Thread(target=download_file, args=("ๆช”ๆกˆ2",))
t1.start(); t2.start()
t1.join(); t2.join()

end = time.time()
print(f"็ธฝๅ…ฑ่€—ๆ™‚๏ผš{end - start:.2f} ็ง’")
Python

Output of the threaded I/O example

Here's a timeline of what happened in this example.

ๆ™‚้–“่ปธ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’
ไปปๅ‹™1: โ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆ             (ๅŸท่กŒ -> ็ญ‰ๅพ… -> ๅŸท่กŒ -> ็ญ‰ๅพ… -> ๅŸท่กŒ)
ไปปๅ‹™2:    โ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆ          (็จๅพŒ้–‹ๅง‹๏ผŒไนŸๆ˜ฏไบค้Œฏ็ญ‰ๅพ…)
็ธฝ่€—ๆ™‚: ็ด„ 2 ็ง’๏ผˆๅ…ฉไปปๅ‹™้‡็–Š็ญ‰ๅพ…๏ผŒไธ้œ€็ญ‰ๅพ…ๅ…ฉๆฌก๏ผ‰
Text

CPU-bound work with multiprocessing

from multiprocessing import Process
import time

def heavy_calculation(name):
    print(f"{name} ้–‹ๅง‹่จˆ็ฎ—...")
    total = sum(range(10**7))
    print(f"{name} ่จˆ็ฎ—ๅฎŒๆˆ๏ผŒ็ธฝๅ’Œ็‚บ {total}")

if __name__ == "__main__":
    start = time.time()

    p1 = Process(target=heavy_calculation, args=("ไปปๅ‹™1",))
    p2 = Process(target=heavy_calculation, args=("ไปปๅ‹™2",))

    p1.start()
    p2.start()
    p1.join()
    p2.join()

    end = time.time()
    print(f"็ธฝๅ…ฑ่€—ๆ™‚๏ผš{end - start:.2f} ็ง’")
Python

Output of the multiprocessing example

Here's the corresponding timeline.

ๆ™‚้–“่ปธ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’
ไปปๅ‹™1: โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ          (ๅฏ†้›† CPU ่จˆ็ฎ—ไธไธญๆ–ท)
ไปปๅ‹™2: โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ          (ๅฆไธ€้ก†ๆ ธๅฟƒๅŒๆ™‚้‹่กŒ)
็ธฝ่€—ๆ™‚: ็ด„ ? ็ง’๏ผˆไพ็…งๅฏฆ้š›็จ‹ๅผ็ขผ่จˆ็ฎ—็จ‹ๅบฆ่€Œๅฎš๏ผ‰
Text

What if we use threads for CPU-bound work?

The examples assume a standard CPython build with the Global Interpreter Lock (GIL). Multiple threads can take turns, but only one thread executes Python bytecode at a time. Switching between them adds overhead to CPU-bound work.

NOTE

Why does CPython use the GIL?

The GIL simplifies parts of the interpreter's memory management and threading model.

Reference counting

  • CPython uses reference counts to keep track of references to an object.
  • Simultaneous updates to a reference count can create race conditions unless they're coordinated.
  • A global lock serializes these operations in a standard GIL-enabled build.

What if we use multiprocessing for I/O-bound work?

Processes can also wait on I/O concurrently. Each process has its own Python interpreter, so the GIL does not prevent different processes from making progress. Starting processes, managing separate memory, and serializing data all add overhead. For work dominated by waiting, threads are usually a lighter option.

How does asyncio fit in?

asyncio offers another way to handle concurrent I/O. It runs coroutines on an event loop.

Example

import asyncio

async def download_file(name):
    print(f"{name} ้–‹ๅง‹ไธ‹่ผ‰...")
    await asyncio.sleep(2)  # ้žๅŒๆญฅ็ญ‰ๅพ…
    print(f"{name} ไธ‹่ผ‰ๅฎŒๆˆ")

async def main():
    await asyncio.gather(
        download_file("ๆช”ๆกˆ1"),
        download_file("ๆช”ๆกˆ2")
    )

asyncio.run(main())
Text

Output of the asyncio example

Here's how the work is interleaved.

ๆ™‚้–“่ปธ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’ โ†’
ไปปๅ‹™1: โ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆ             (้–‹ๅง‹ๅพŒ้ฆฌไธŠ await๏ผŒ็›ดๅˆฐ็ตๆŸ)
ไปปๅ‹™2:  โ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–ˆโ–ˆโ–ˆ            (ๅนพไนŽๅŒๆ™‚้–‹ๅง‹๏ผŒไนŸ await๏ผŒ็›ดๅˆฐ็ตๆŸ)
็ธฝ่€—ๆ™‚: ็ด„ 2 ็ง’
Text
  • Coroutines yield control to the event loop when they await an operation.
  • Both tasks can start and finish close together because one coroutine can make progress while the other waits.
  • CPU-bound work still occupies the event loop until it yields or completes.

Choosing a tool

WorkloadOptionWhy
I/O-boundthreading or asyncioOther tasks can run during an I/O wait
Many lightweight I/O tasksasyncioThe event loop schedules coroutines without a separate thread for each task
CPU-boundmultiprocessingProcesses can run Python work across multiple cores
MixedCombine strategiesconcurrent.futures provides thread and process executors