# Python multiprocessing vs threading

> Threads share one process and the GIL; multiprocessing spawns processes so CPU work can use more cores.

Source: https://fullbytedeveloper.com/blog/python-multiprocessing-vs-threading
Published here: 2026-12-03, updated 2026-12-03

Threading shares one process; multiprocessing starts more processes. CPython's GIL keeps that difference painful for CPU-bound Python, so pick the module that matches the bottleneck [1] [2] [3]. Pair with [Python GIL: why threads are not truly parallel](/blog/python-gil-threads-not-parallel) and [concurrency vs parallelism](/blog/concurrency-vs-parallelism-plain).

## Quick start: two threads, two processes

Save as `mp_thread_demo.py` and run with Python 3:

```python
import threading
import time

def wait_label(name):
    time.sleep(0.2)
    print(name)

t0 = time.time()
t1 = threading.Thread(target=wait_label, args=("a",))
t2 = threading.Thread(target=wait_label, args=("b",))
t1.start(); t2.start()
t1.join(); t2.join()
print("threads elapsed", round(time.time() - t0, 2))
```

On a draft Linux host (Python 3.12), output:

```text
a
b
threads elapsed 0.2
```

Both labels appear after roughly one sleep window, not two stacked serial sleeps [1]. That is concurrency while blocked on I/O, not proof that threads parallelize CPU.

CPU side with processes:

```python
from multiprocessing import Process
import time

def burn(n):
    x = 0
    for i in range(n):
        x += i * i
    print("sum", x)

if __name__ == "__main__":
    t0 = time.time()
    jobs = [Process(target=burn, args=(500_000,)) for _ in range(2)]
    for j in jobs: j.start()
    for j in jobs: j.join()
    print("processes elapsed", round(time.time() - t0, 2))
```

Same host, guarded with `if __name__ == "__main__":` [2]:

```text
sum 41666541666750000
sum 41666541666750000
processes elapsed 0.03
```

Wall time drops versus one process doing both loops on a multi-core host because two interpreters run bytecode in parallel [2]. Numbers scale with core count and loop size; treat this as shape, not a benchmark gospel.

## Draw the GIL in one picture

<figure aria-label="One process with threads sharing one GIL beside two processes with separate interpreters">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 220" role="img" aria-hidden="true">
  <rect x="0" y="0" width="360" height="220" rx="12" fill="#0b1f3a"/>
  <rect x="15.4286" y="4" width="154.286" height="200" rx="4" fill="#123052" stroke="#14b8a6" stroke-width="1.28571"/>
  <text x="92.5714" y="46" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">one process [1]</text>
  <circle cx="45" cy="86" r="18" fill="none" stroke="#14b8a6" stroke-width="0.771429"/>
  <text x="45" y="90" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">T1</text>
  <circle cx="77.1429" cy="86" r="18" fill="none" stroke="#14b8a6" stroke-width="0.771429"/>
  <text x="77.1429" y="90" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">T2</text>
  <circle cx="109.286" cy="86" r="18" fill="none" stroke="#14b8a6" stroke-width="0.771429"/>
  <text x="109.286" y="90" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">T3</text>
  <rect x="51.4286" y="114.5" width="82.2857" height="35" rx="3" fill="#123052" stroke="#14b8a6" stroke-width="1.28571"/>
  <text x="92.5714" y="138" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">GIL [3]</text>
  <text x="92.5714" y="168" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">one bytecode runner</text>
  <rect x="190.286" y="32" width="72" height="80" rx="4" fill="#123052" stroke="#14b8a6" stroke-width="1.28571"/>
  <text x="226.286" y="64" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">process A [2]</text>
  <text x="226.286" y="82" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">own GIL</text>
  <rect x="272.571" y="112" width="72" height="80" rx="4" fill="#123052" stroke="#14b8a6" stroke-width="1.28571"/>
  <text x="308.571" y="144" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">process B [2]</text>
  <text x="308.571" y="162" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">own GIL</text>
  <text x="180" y="206" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">CPU-bound Python: processes; I/O waits: threads [1] [3]</text>
</svg>
</figure>

The glossary defines the GIL as a mutex that allows only one thread to execute Python bytecode at a time [3]. Threads still help when work spends time in I/O or in C extensions that release the lock during heavy native work [3] [1].

## When threading wins

The `threading` module implements operating-system threads in the standard library [1]. Shared memory makes passing large in-memory structures cheap and makes data races likely without locks [1]. Use threads when tasks mostly wait on sockets, disks, or `time.sleep`, or when you fan out blocking API calls with `concurrent.futures.ThreadPoolExecutor` [4].

Watch shared mutable globals. The GIL does not serialize your Python logic; it serializes bytecode execution. Two threads incrementing the same counter without a lock still races.

## When multiprocessing wins

`multiprocessing` mirrors the threading API with `Process` objects, but each process has its own interpreter [2]. That design exists so you can use multiple CPUs despite the GIL [3]. Costs the docs emphasize: slower startup, duplicated memory for imported modules, and queues or pipes for IPC [2].

Always protect the entry point on Windows and macOS default spawn start method [2]:

```python
if __name__ == "__main__":
    ...
```

Forgotten guards create fork bombs in notebooks and double-executed `main` blocks.

`multiprocessing.Pool` and `ProcessPoolExecutor` batch embarrassingly parallel maps. Profile before you pick pool size; oversubscribing RAM hurts more than idle cores.

## Leave asyncio on the I/O lane

Asyncio cooperates on one thread with an event loop. It is not a GIL workaround for tight Python loops. See [python async def is lazy](/blog/python-async-def-is-lazy). You can mix models carefully (async main plus executor for blocking calls) but not by spawning threads for CPU and hoping.

## Measure before you swap modules

`cProfile` or `py-spy` on one representative input tells you whether time sits in `time.sleep`, `recv`, or a tight Python for-loop [3]. If the profile is wall-clock blocked on I/O, threads or asyncio deserve the first experiment [1]. If the profile is bytecode in user code, spawn a second process and compare total time including fork [2].

Multiprocessing duplicates memory for large parent heaps. Passing a ten-megabyte structure through `Queue` pickles it; passing a read-only blob might belong in `multiprocessing.shared_memory` per the docs when you outgrow toy examples [2].

The GIL can release during some C extension calls [3]. NumPy dot products may parallelize inside native code while your Python orchestration stays single-threaded for bytecode. Profile the actual library, not an imaginary pure-Python loop.

## Pick a rule without dogma

| Symptom | Likely module |
|---|---|
| Flamegraph stuck in `socket.read` | threading or asyncio [1] |
| Flamegraph stuck in pure Python loops | multiprocessing [2] |
| Heavy work in NumPy/C extension | measure first; extension may release GIL [3] |
| Need shared in-memory state | threading with locks, or redesign IPC [1] [2] |

For the OS isolation story behind processes, see [threads vs processes](/blog/threads-vs-processes). The vocabulary for overlapping work without claiming every core is in [concurrency vs parallelism](/blog/concurrency-vs-parallelism-plain).

## Logging and debugging across children

Child processes inherit the parent's file descriptors unless you configure logging to reopen files [2]. `print` from children can interleave on stdout; use the logging module with process names when debugging pool workers.

Jupyter notebooks that call `multiprocessing` without `if __name__` guards are a common classroom footgun [2]. Run the same code as a script when teaching spawn rules.

## ProcessPoolExecutor in one block

When you want a pool without managing `Process` objects directly:

```python
from concurrent.futures import ProcessPoolExecutor

def square(n):
    return n * n

if __name__ == "__main__":
    with ProcessPoolExecutor(max_workers=2) as ex:
        print(list(ex.map(square, range(5))))
```

You should see `[0, 1, 4, 9, 16]`. Same spawn rules as raw `Process` [2] [4].

## Where people go wrong

Spawning twenty threads to speed up a pure-Python matrix multiply. The GIL keeps one bytecode runner [3].

Using `multiprocessing` for tiny tasks where spawn cost dominates wall time [2].

Forking on Linux with threads already running in the parent (subtle deadlock class). Prefer spawn on cross-platform code or read the platform notes in the multiprocessing guide [2].

`asyncio` replaces multiprocessing for CPU work.

Sharing large objects by pickling them on every queue send without measuring copy cost [2].

Running CPU work in `ThreadPoolExecutor` and blaming Python for "no speedup."

## Free-threaded CPython note

Upstream work on optional GIL disabling in CPython changes the table for some builds [3]. Until you deliberately install and test a free-threaded interpreter, assume the GIL story above for the default `python3` on your PATH. Re-profile after any interpreter swap.

## Daemon threads and shutdown

The threading docs describe daemon threads that exit when the main thread ends [1]. Background workers must finish critical sections or mark themselves non-daemon before process exit. Multiprocessing children require explicit `join` or pool shutdown [2]. Otherwise you lose log lines and half-written files silently.

Queue backpressure matters when producers outrun consumers [2]. An unbounded queue hides memory growth until the OOM killer arrives. Size queues to match consumer throughput and monitor RSS during load tests.

Run `mp_thread_demo.py` once with `time` and log whether threads or processes won on your box. When the profile still shows bytecode stuck under one mutex, read [Python GIL: why threads are not truly parallel](/blog/python-gil-threads-not-parallel) for the longer mutex story before you spawn another `ThreadPoolExecutor`. [Go without async await](/blog/go-without-async-await) is a useful contrast when someone asks why another language treats CPU and I/O concurrency differently by default. For streaming work without loading the whole dataset, see [Python yield and iterators](/blog/python-yield-iterators).

Send this to the friend who spins threads for pure Python CPU work under the GIL.

Image: Left pane with a gold ring filtering teal and gold lines; right stacked panes each holding a teal block with looped arrows, no people and no readable text.
https://fullbytedeveloper.com/media/python-multiprocessing-vs-threading/python-multiprocessing-vs-threading-venice.webp
Threads share one GIL; processes get separate interpreters.

## Sources

1. [Python docs: threading, accessed 2026-12-03](https://docs.python.org/3/library/threading.html)
2. [Python docs: multiprocessing, accessed 2026-12-03](https://docs.python.org/3/library/multiprocessing.html)
3. [Python docs: Global Interpreter Lock, accessed 2026-12-03](https://docs.python.org/3/glossary.html#term-global-interpreter-lock)
4. [Python docs: concurrent.futures, accessed 2026-12-03](https://docs.python.org/3/library/concurrent.futures.html)
5. [Full Byte Developer: Python GIL threads not parallel](https://fullbytedeveloper.com/blog/python-gil-threads-not-parallel)
6. [Full Byte Developer: Concurrency vs parallelism plain](https://fullbytedeveloper.com/blog/concurrency-vs-parallelism-plain)
7. [Full Byte Developer: Python async def is lazy](https://fullbytedeveloper.com/blog/python-async-def-is-lazy)
