๐ Python Mistakes Everyone Makes ❌
Day 44: Using Threads for CPU-Bound Tasks
Threads in Python feel like the obvious way to make programs faster.
But when it comes to CPU-bound work, threads often do the opposite.
❌ The Mistake
Using threads to speed up heavy computation.
import threadingdef work():total = 0for i in range(10_000_000):total += ithreads = [threading.Thread(target=work) for _ in range(4)]for t in threads:t.start()for t in threads:
t.join()
This looks parallel — but it isn’t.
❌ Why This Fails
Python has a Global Interpreter Lock (GIL)
Only one thread runs Python bytecode at a time
CPU-bound threads cannot execute in parallel
Thread context-switching adds overhead
Performance may be worse than single-threaded code
๐ง What Threads Are Actually Good For
Threads work well for:
Network requests
File I/O
Waiting on external resources
They are not meant for heavy computation.
✅ The Correct Way
Use multiprocessing for CPU-bound tasks.from multiprocessing import Pooldef work(n):total = 0
for i in range(n):total += ireturn totalif __name__ == "__main__":with Pool(4) as p:
p.map(work, [10_000_000] * 4)
Each process:
Has its own Python interpreter
Has its own GIL
Runs truly in parallel on multiple cores
๐ง Simple Rule to Remember
๐ Threads for I/O-bound work
๐ Processes for CPU-bound work
๐ Final Takeaway
If your program is doing heavy computation, threads won’t save you.
Understanding the GIL helps you choose the right tool — and avoid wasted effort.
Write smarter, faster Python ๐⚡


0 Comments:
Post a Comment