Subinterpreters in Python: Real Parallelism, without the Multiprocessing Hassle For a while now, Python’s parallelism options have been: - Threading: “Parallel” work, but the GIL slows you down anyway. - Multiprocessing: Separate memory space, and often, a “why am I writing a system program in Python” feeling. And now, with Python 3.12+ and subinterpreters, there’s a third option: multiple Python interpreters in a single process, with their own GIL, their own modules, and none of the guilt that comes with creating a new process. What You Actually Get - Real parallelism for your CPU-bound tasks, without going all out with multiprocessing. - Less overhead, since there’s no process fork, and less memory usage. - Better isolation from threads, without the overhead of having separate processes. - And, of course, asyncio for I/O-bound tasks, and offloading CPU-bound tasks into subinterpreters, which feels like a lovely “async+parallel” combination in a single app. When It Makes Sense - Realistically, subinterpreters are a good option when you want parallelism without going out and creating a distributed system. - Realistically, subinterpreters are a good option when startup time and memory usage are more important than “let’s create ten processes as a design decision.” - Realistically, subinterpreters are a good option when you want better isolation than threads, without going all out with separate processes. A Few Caveats - It’s a relatively new feature, and there might be changes in future Python versions (3.13+, free-threading). - Not all C extensions are ready for subinterpreters out of the box and might require some tweaking.
Python Subinterpreters for Parallelism
More Relevant Posts
-
We often get asked the question - how does Pyrefly compare to other Python type checkers out there? 🐍 🤔 While there isn’t a simple answer to that question, one concrete example where type checkers behave differently is empty container inference. In this week’s blog Pyrefly maintainers Danny Yang and Jia Chen break down what that means for you: 👉 https://lnkd.in/eUE-7ajX #python #pyrefly #typechecking #opensource
To view or add a comment, sign in
-
🚀 Advanced Python Tips #6 — multiprocessing.Pool “Python is slow.” No. Your execution model might be. A for loop in Python is optimized in C and is surprisingly efficient. The real limitation isn’t iteration speed; it’s synchronous execution. If you're running CPU-bound tasks sequentially, you're leaving multiple CPU cores idle. Here’s the uncomfortable truth many developers gloss over: 👉 The GIL prevents true parallelism with threads for CPU-bound workloads. 👉 multiprocessing, however, does not share the GIL across processes. If your tasks are CPU-intensive and independent, multiprocessing.Pool enables real parallelism. With Pool: - Each process has its own Python interpreter - Each process has its own GIL - Work is distributed across multiple CPU cores - You get true parallel execution But here’s the part that rarely makes it into tutorials: ⚠️ multiprocessing has non-trivial overhead (process spawn + pickling) ⚠️ For lightweight tasks, it can be slower than a simple for loop ⚠️ For I/O-bound workloads, asyncio or threading may be more efficient Multiprocessing isn’t a magic performance switch. It’s a tool, and it only shines in the right context. It makes sense when your workload is: - CPU-bound - Independent - Heavy enough to amortize process overhead Otherwise, you’re just parallelizing the wrong bottleneck. Python isn’t slow. Misapplied parallelism is. Do you analyze whether your bottleneck is CPU-bound or I/O-bound before parallelizing?
To view or add a comment, sign in
-
-
What is a Function in Python? A function is a block of reusable code that performs a specific task. Instead of writing the same code again and again, you write it once inside a function and call it whenever needed. 👉 Think of it like a machine: You give input It processes It gives outputHow to Create (Define) a Function In Python we use the keyword: "def" Basic Syntax def function_name(): # code block Example 1: Simple Function (No Parameters) def greet(): print("Hello Venkat") greet() Output: Hello Venkat Explanation: def greet() → defining function greet() → calling function Example 2: Function With Parameters def greet(name): print("Hello", name) greet("Venkat") Output: Hello Venkat Here: name is parameter "Venkat" is argument Example 3: Function With Return Value def add(a, b): return a + b result = add(10, 20) print(result) Output: 30 Important: return sends value back Without return, function gives None Types of Functions Built-in functions print() type() len() User-defined functions Created using def Lambda functions (anonymous) Example: Lambda Function add = lambda a, b: a + b print(add(5, 3)) Output: 8 *Real Example (Practical):: def check_even(number): if number % 2 == 0: return "Even" else: return "Odd" print(check_even(10)) Output: Even
To view or add a comment, sign in
-
-
After ~6 years of building with Python, I still get surprised by how much depth there is in the “basic” stuff. I recently read an article on Python memory management (from stack frames to closures) and it genuinely taught me a lot. A few things that really clicked for me: • Garbage collection vs references (finally cleared up): I’ve used Python’s GC forever, but this article explains the relationship between reference counting, object references, and cycle detection in a way that removed a bunch of mental fog. It made it easier to reason about why objects stick around, when they get freed, and why “it should be collected” isn’t always true the way we casually assume. • Stack frames, scopes, and what actually stays alive: The framing around stack frames helped connect execution flow to memory behavior—especially how local variables, function calls, and references interact under the hood. • Closures, cell objects, and the “ohhh that’s why” moment: I knew closures existed, but I wasn’t aware of cell objects and the mechanism Python uses to keep variables alive for inner functions. That was new to me—and honestly one of those concepts that makes you write better, safer code once you understand it. What I liked most: this is one of the few articles I’ve read recently that covers a very foundational concept—but still manages to teach a lot without getting hand-wavy. If you write Python professionally (or even casually), it’s worth reading—especially if you’ve ever had questions like: • “Why didn’t this object get freed yet?” • “What exactly does GC do here?” • “How do closures really store state?” https://lnkd.in/dZSi27vn #Python #SoftwareEngineering #BackendEngineering #Programming #ComputerScience #MemoryManagement
To view or add a comment, sign in
-
Just published my beginner-friendly guide on Python 🔥 Confused between Lists and Tuples? When to use which? And WHY it matters in interviews 👀 I explained it with simple real examples (no boring theory) Read here 👇 https://lnkd.in/dfwe-Cc6 Special thanks to @Innomatics Research Labs for the learning opportunity 🙌 #Python #Programming #CodingForBeginners #DataStructures #Innomatics
To view or add a comment, sign in
-
My language is faster than Python!! In a few areas, at least. I recently started focusing seriously on optimization. For a long time I deliberately avoided it because I knew it would be difficult. When I first benchmarked matrix multiplication (50×50): Python: 0.023 seconds Luna: 6.7 seconds Seeing how much My language was lacking I decided to start optimising. I moved matrix multiplication into a dedicated C backend and implemented SIMD optimizations. After refactoring memory layout and reducing overhead, I reran the benchmark. For 200×200 matrix multiplication: Native Python : 2.35 seconds NumPy : 0.045 seconds Luna (C/SIMD backend): 0.0169 seconds That’s a 139× improvement over native Python for this specific compute-heavy workload. I particularly focused on this and was dead set to make it faster without adding complexity to Luna. However, this does not mean Luna is universally faster than Python. NumPy still dominates vector operations. Python’s runtime is significantly faster for variable lookups. Below is the image of benchmark.I will provide the documentation and Sample code I used for testing so anyone can test it. https://lnkd.in/gA98YHD8
To view or add a comment, sign in
-
-
✅ Python for loop — Explanation A for loop in Python is used to repeat a block of code multiple times. 👉 It is mainly used when you know how many times you want to run the loop. Instead of writing the same code again and again, a for loop automatically repeats it. --- ⭐ Syntax of for loop for variable in sequence: # block of code variable → stores each value one by one sequence → list, string, tuple, range, etc. --- ✅ Example 1 — Print numbers 1 to 5 for i in range(1, 6): print(i) 👉 Output: 1 2 3 4 5 --- ✅ Example 2 — Print a string characters name = "Python" for ch in name: print(ch) 👉 Output: P y t h o n --- ✅ Example 3 — Print list items numbers = [10, 20, 30, 40] for n in numbers: print(n) --- ✅ Example 4 — Sum of numbers using for loop sum = 0 for i in range(1, 6): sum = sum + i print("Sum =", sum) 👉 Output: Sum = 15 --- ⭐ Advantages of for loop ✔ Repeats code automatically ✔ Easy to use ✔ Less code (saves time) ✔ Works with lists, strings, range, etc. --- If you want, I can also give: ✅ for loop programs list (10–20 examples) ✅ for loop with patterns ✅ for loop interview programs ✅ for vs while loop difference Just tell me 👍
To view or add a comment, sign in
-
Day 2nd ->I started by understanding what Python is and why it’s so popular. Python’s simple syntax, readability, and massive ecosystem of libraries . * Next, I learned how "Python executes code" The process is straightforward but fascinating: 👉 You write the code → Python compiles it into bytecode → the interpreter executes it → and finally, you see the output. This behind-the-scenes flow helped me understand why Python is called an interpreted language. *I also explored "comments" and "print formatting", which are essential for writing clean code. Comments make programs readable for humans, while the "print() function" becomes more powerful with parameters like sep and end, allowing better control over output formatting. *Then came "data types" the building blocks of any program. I worked with: Integers and Floats for numbers Strings and Characters for text Booleans for true/false logic Understanding data types clarified how Python stores and processes different kinds of information. * learned about "variables" and the concept of "reinitialization" which allows changing a variable’s value anytime—simple, flexible, and very Pythonic. *Finally, I studied "identifier rules" which define how variables should be named. Following these rules ensures clarity, avoids errors, and makes code professional and readable.
To view or add a comment, sign in
-
*Async Python: When Not to Use It* Async Python is a way to run multiple tasks concurrently without waiting for one to finish before starting the next. Python can switch between tasks, making it efficient. It's built on async, await, and event loops. It's powerful, but don't just use it without thinking 😅. *When Not to Use Async* 1. *CPU-bound tasks*: If you're doing heavy calculations, image processing, or AI/ML training, async won't help. It might even slow you down. Use multiprocessing or optimized libraries instead. 2. *Simple apps*: If your app is small with few requests and minimal background work, you don't need async. Synchronous code is easier to read and debug. 3. *Your team isn't familiar with async*: Async code can be clean until it breaks. Debugging becomes painful if your team doesn't understand event loops, await chains, or blocking vs non-blocking calls. 4. *Blocking libraries*: Many Python libraries aren't async-safe. If you call blocking code inside an async function, your app will behave like a slow app. 5. *Predictability is crucial*: Async execution isn't obvious. Stack traces are messy, and errors are random. For critical systems, simplicity is better. *The Bottom Line* Async Python is a scalability tool, not a speed booster. Don't use it just because it sounds advanced. Use it when you have plenty of I/O tasks and you've identified a bottleneck. Master synchronous Python first. Async can come later, or maybe you won't need it at all. 🌀 #python #backenddevelopment #programming
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