Question №40
Remaining:
What is the difference between threading and multiprocessing in Python?
Sample Answer
Show Answer by Default
Multithreading (threading):
Threads operate within a single process and share the same memory space:
import threading def download(url): print(f"Downloading {url}") threads = [] for url in ["url1", "url2", "url3"]: t = threading.Thread(target=download, args=(url,)) threads.append(t) t.start() for t in threads: t.join() # Wait for all threads to finish
Multiprocessing (multiprocessing):
Each process gets its own memory space and its own Python interpreter:
from multiprocessing import Process def heavy_computation(n): return sum(i * i for i in range(n)) processes = [] for n in [10_000_000, 20_000_000]: p = Process(target=heavy_computation, args=(n,)) processes.append(p) p.start() for p in processes: p.join()
The GIL (Global Interpreter Lock):
The GIL is a CPython mechanism that allows only one thread to execute Python bytecode at a time. As a result:
- Threads do not speed up tasks performing heavy calculations (CPU-bound tasks).
- Threads do speed up tasks waiting on external events (I/O-bound tasks): network requests, reading files.
When to use which:
- threading — for I/O-bound tasks: file downloads, API calls, database queries.
- multiprocessing — for CPU-bound tasks: data processing, complex mathematical calculations, and algorithms.
