Threading vs Multiprocessing
Python supports concurrency using threads and processes. Choosing the right one improves performance.
Threading
import threading
def task():
print("Thread running")
t = threading.Thread(target=task)
t.start()
Multiprocessing
import multiprocessing
def task():
print("Process running")
p = multiprocessing.Process(target=task)
p.start()
Key Differences
✔ Threads share memory
✔ Processes use separate memory
✔ GIL affects threading
✔ Processes use separate memory
✔ GIL affects threading
When to Use What?
- I/O-bound → Threading
- CPU-bound → Multiprocessing