Quick Python Tip: Multithreading Threading vs Multiprocessing: The Python Tip That Finally Made Everything Click Some use threading thinking it would “make things faster.” Sometimes it did… and sometimes it made everything worse Here’s the simple rule they wish they learned earlier: If you’re waiting -> use threading If you’re computing -> use multiprocessing That’s it. That’s the game-changer. Threading is bad for: - Heavy calculations - Data / image / video processing - ML workloads Threading is great for: - API calls - File & DB I/O - Network requests - Web scraping Modern Python move 👇 - Use ThreadPoolExecutor, not manual `Thread()` - Set a smart pool size (10–20 threads for most I/O) - Always use timeouts - Process results with as_completed() - Avoid shared state when possible Big reality check: Python’s GIL means threads don’t run CPU code in parallel. So threading ≠ faster math. It’s just better at waiting. Your decision framework now: • Waiting on I/O → Threading • Crunching CPU → Multiprocessing • Both? → Multiprocessing + threading inside Tip Summary: Stop creating threads manually. Start managing concurrent I/O the right way. Have you ever used threading expecting speed… and got nothing? What was the task?
Threading vs Multiprocessing: Python Performance Tip
More Relevant Posts
-
I built this Python library because I ran into a problem myself. We had a solid RAG setup. Retrieval was good. It pulled relevant info. The answers looked fine. But when it came to decisions, it felt… inconsistent. A user I’ve been working closely with told me: - “It finds the right info.” - “But the judgment sometimes changes.” Same kind of incident. Slightly different phrasing. Different output. Around that time I was learning Graph Data Structures and just randomly thought what if decision memory shouldn’t be probabilistic? So for internal use, I tried modeling: Incident → Root Cause → Decision → Outcome Explicit relationships but Deterministic retrieval. So whatever goes in, that only comes out. RAG is still there but I am not trying to overthrow vector search. It is great for retrieval. But for decision memory, structure matters. After we deployed this update, they actually preferred this approach for consistent judgment. So I cleaned it up a bit and open-sourced it. It is live as a Python library. Please try it out. (Check the comments)
To view or add a comment, sign in
-
Python is entering a new era with Free-Threading — and it could fundamentally change how we build high-performance applications. For decades, Python developers have been limited by the Global Interpreter Lock (GIL). The GIL ensured thread safety by allowing only one thread to execute Python bytecode at a time, which meant CPU-bound workloads could not truly run in parallel on multiple cores. This is why frameworks handling heavy concurrency often relied on: • multiprocessing • async I/O • external workers But Free-Threaded Python (introduced experimentally in Python 3.13 under PEP 703) aims to remove the GIL. What does this mean in practice? 1️⃣ True parallelism in multithreading Multiple threads can execute Python code simultaneously across CPU cores. 2️⃣ Better CPU utilization Compute-heavy workloads like AI inference, simulations, and data processing can scale more naturally. 3️⃣ Frameworks like FastAPI get even stronger FastAPI already uses an async event loop for I/O concurrency. With free-threading, CPU tasks handled in threads can finally scale across cores without the GIL bottleneck. 4️⃣ Simpler concurrency model Developers may rely more on threads instead of complex multiprocessing architectures. However, there are still challenges: • Many C extensions assume the GIL exists • Libraries must become thread-safe • Ecosystem migration will take time But the direction is clear: Python is evolving from "concurrency through workarounds" to true parallel execution. For backend engineers building high-throughput APIs, data pipelines, and AI systems — this is a major shift worth paying attention to. The Python ecosystem might look very different in the next few years. #Python #SoftwareEngineering #BackendDevelopment #FastAPI #Concurrency #Multithreading #PEP703 #Programming
To view or add a comment, sign in
-
Using Python Functions as Robot Framework Keywords Robot Framework loads Python functions as keywords through a simple mapping layer. Resources/utils/mysql_keywords.robot *** Settings *** Library library/mysql_util.py *** Keywords *** Initialize Results Table Create Results Table Write Result To Database [Arguments] ${id} ${name} ${code} ${status} Insert Result ... ${id} ... ${name} ... ${code} ... ${status} Robot automatically converts: create_results_table → Create Results Table insert_result → Insert Result No extra configuration required. This allows database operations to be reused across tests. In the final post, I’ll show the full test execution flow.
To view or add a comment, sign in
-
Python isn't just a programming language it's a superpower! And the best part? There's a library for everything you want to build. This image is a visual guide to some of the most powerful Python libraries and what they do: •Pandas - Data manipulation & analysis •TensorFlow - Deep learning & Al •Matplotlib - Data visualization • Seaborn - Advanced statistical charts •BeautifulSoup - Web scraping •Selenium - Browser automation •FastAPI - High-performance APIs Whether you're into data science, automation, or backend development Python has you covered. PYTHON FOR EVERYTHING
To view or add a comment, sign in
-
-
🐍 Python Sets — Store Unique Values Only 🔹 Sets are unordered collections that automatically remove duplicates. Perfect for when you only want unique items 👇 # Create sets directly number = {1, 2, 3, 4} # Create set from a list fruit = set(["apple", "banana", "orange"]) # Remove duplicates from a list score = [85, 23, 53, 85, 33] unique_score = set(score) print(unique_score) ✅ Output (order may vary): {33, 85, 53, 23} 💡 Beginner Explanation ✔️ number = {1,2,3,4} → Simple set with numbers ✔️ fruit = set([...]) → Convert a list to a set ✔️ unique_score = set(score) → Remove duplicate values from a list 🔑 Key Features of Sets • Only stores unique values • Unordered → cannot access by index • Useful for removing duplicates, membership checks, and set operations 🔥 Example Use Case: students = ["Ali", "Sara", "Ali", "Danial"] unique_students = set(students) print(unique_students) # Output: {'Ali', 'Sara', 'Danial'} 🚀 Sets make your Python code cleaner when working with unique data. #Python #Coding #Programming #LearnToCode #Developer
To view or add a comment, sign in
-
Confession: I thought sets were useless until I had a production meltdown at midnight. Duplicate emails everywhere. Lists crawling at scale. Three hours of debugging that set() fixed in ten minutes. This post covers: → Cleaning 50,000 emails without loops → Finding overlapping customers instantly → Why sets are 2000x faster for lookups → Real data pipeline examples Wrote everything I learned – the performance gains, the real use cases, the moments where sets save your sanity. If you're still using lists for everything, this might help. 📖 https://lnkd.in/gZ2y3mEa What Python feature did you learn way too late? 👇 #Python #DeveloperTips #CodingJourney #TechBlog #Coding #LearnToCode #DataStructures Innomatics Research Labs
To view or add a comment, sign in
-
Threading feels fast until you realize Python only uses it well for waiting, not heavy computing, so use threads for I/O, processes for CPU, and stop expecting math to get faster just because you added threads.
AI Engineer (GenAI & Agentic Systems) | Building LLM Apps, RAG Systems & AI Agents | Python • FastAPI
Quick Python Tip: Multithreading Threading vs Multiprocessing: The Python Tip That Finally Made Everything Click Some use threading thinking it would “make things faster.” Sometimes it did… and sometimes it made everything worse Here’s the simple rule they wish they learned earlier: If you’re waiting -> use threading If you’re computing -> use multiprocessing That’s it. That’s the game-changer. Threading is bad for: - Heavy calculations - Data / image / video processing - ML workloads Threading is great for: - API calls - File & DB I/O - Network requests - Web scraping Modern Python move 👇 - Use ThreadPoolExecutor, not manual `Thread()` - Set a smart pool size (10–20 threads for most I/O) - Always use timeouts - Process results with as_completed() - Avoid shared state when possible Big reality check: Python’s GIL means threads don’t run CPU code in parallel. So threading ≠ faster math. It’s just better at waiting. Your decision framework now: • Waiting on I/O → Threading • Crunching CPU → Multiprocessing • Both? → Multiprocessing + threading inside Tip Summary: Stop creating threads manually. Start managing concurrent I/O the right way. Have you ever used threading expecting speed… and got nothing? What was the task?
To view or add a comment, sign in
-
-
Did you know that Python sets can automatically remove duplicates and significantly improve lookup performance? In this blog, I explained: ✔️ What sets are ✔️ How they use hashing ✔️ Real-world examples (email cleaning, product IDs) ✔️ Union, Intersection & Difference operations ✔️ When NOT to use sets If you're learning Python or working with data, understanding sets can greatly improve your coding efficiency. 🔗 Read the full blog here: [https://lnkd.in/dc_2_NAp] #Python #DataStructures #Programming #Coding #PythonBeginner #DataCleaning #LearnPython #Innomatics Research Labs
To view or add a comment, sign in
-
Why Python Handles Data Faster Than You Think 🚀 “Python is slow.” That’s the common assumption. But in real-world data engineering and ML workloads, Python often performs far better than expected. Here’s why 👇 1️⃣ Python Doesn’t Work Alone When you use: -NumPy -Pandas -PyArrow You’re executing highly optimized C/C++ and Fortran code under the hood. Python acts as the orchestrator — not the heavy lifter. 2️⃣ Vectorization > Loops Operations like: df["price"] * 2 can be 10–100x faster than manual iteration. Why? Because they run at the native level — avoiding Python loop overhead entirely. 3️⃣ The Modern Python Data Stack Is Built for Scale Tools that dramatically improve performance: • Polars – Rust-powered, extremely fast • Dask – Parallel & distributed computing • Modin – Scales Pandas automatically • Numba – JIT compilation for speed • Vaex – Efficient large dataset processing • Cython – Compile Python to C Python isn’t winning because of raw interpreter speed. It wins because of its ecosystem. 4️⃣ Speed = Time to Solution In production systems, performance matters. But so does: -Development speed -Debugging speed -Deployment speed -Hiring availability In real-world engineering, time to solution often matters more than microsecond benchmarks. The biggest mistake? Benchmarking Python loops instead of benchmarking Python libraries. Huge difference. 💬 What’s the largest dataset you’ve handled in Python? #Python #DataEngineering #MachineLearning #BackendDevelopment #Performance #AI
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Before 3.14 that No-GIL wasn't a thing, threading could in some cases be used for heavy computational functions too. If a function is written in extension modules, it can release the GIL. Some famous libraries do that for some of their functions and let other python code runs in parallel. Tensorflow for example: https://github.com/tensorflow/tensorflow/blob/d23a77161813cb194995012dd227ac732335bfa6/tensorflow/python/client/tf_session_helper.cc#L408 Numpy: https://github.com/search?q=repo%3Anumpy%2Fnumpy+NPY_BEGIN_THREADS&type=code