Leanpub Header

Skip to main content

Next Generation Python

Free-Threading, Rust Extensions, and High-Performance Python

Next Generation Python
This book is 100% completeLast updated on 2026-09-07

Python is entering a new performance era. Explore free-threaded execution, Rust extensions and modern tools for building faster, more scalable Python systems. Learn how CPython is changing, when Rust makes sense and how to turn these ideas into production-ready software.

Minimum price

$25.00

$35.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
230
Pages
About

About

About the Book

Python is undergoing its most fundamental performance transformation in decades. Free-threaded execution, mature Rust bindings, and a rapidly evolving performance ecosystem are reshaping what Python can do at scale. This book equips experienced Python developers, software engineers, and systems engineers with the technical depth and practical skills to design, build, and operate high-performance Python systems for the next era. You will understand why Python's concurrency model is changing, how to write code that works efficiently under both GIL and free-threaded modes, how to extend Python with Rust for maximum performance, and how to choose and combine the right tools for your specific workloads. The book covers CPython internals, the Global Interpreter Lock, PEP 703 free-threaded Python, PyO3 and maturin, profiling and benchmarking, and production deployment strategies. Every concept is explained before implementation, with complete, runnable, production-oriented code examples in Python and Rust.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

Free-Threading, Rust Extensions, and High-Performance Python

Introduction

Chapter 1: The Python Performance Problem

  1. What “Slow” Actually Means for Python
  2. The Two Axes of Performance: Throughput and Latency
  3. Why Single-Threaded Python Can No Longer Scale
  4. When “It Works in Python” Is Not Good Enough
  5. The Historical Promise and the Modern Reality

Chapter 2: Inside CPython - How Python Actually Runs

  1. The Python Virtual Machine: Bytecode and the Main Loop
  2. Object Model: Reference Counting and Memory Management
  3. The Cost of Python Objects: Allocation, Layout, and Overhead
  4. How Python Executes: From Source to Machine Code
  5. Where Time Actually Goes: Anatomy of a Python Operation

Chapter 3: The Global Interpreter Lock - Anatomy of a Contention

  1. How the GIL Works: Implementation and Mechanics
  2. Why the GIL Exists: Reference Counting and Thread Safety
  3. What the GIL Actually Prevents (and What It Does Not)
  4. Measuring GIL Contention: Symptoms and Diagnosis
  5. The GIL Through History: Design Decisions That Endured

Chapter 4: Threading in Python - What You Get and What You Lose

  1. How Python Threads Really Work Under the GIL
  2. I/O-Bound Workloads: Where Threading Shines
  3. CPU-Bound Workloads: Where Threading Fails
  4. The Illusion of Parallelism and Its Cost
  5. Thread Safety, Data Races, and Shared Mutable State

Chapter 5: Multiprocessing and Process-Based Parallelism

  1. The Multiprocessing Module: Architecture and Trade-offs
  2. Inter-Process Communication: Queues, Pipes, and Shared Memory
  3. Memory Overhead and Serialization Costs
  4. When Multiprocessing Is the Right Choice
  5. Limitations: Start Methods, Pickling, and Portability

Chapter 6: Asyncio - Cooperative Concurrency

  1. How asyncio Works: Event Loops, Coroutines, and Futures
  2. I/O-Bound Performance: asyncio vs Threading
  3. When async Fails: CPU-Bound Tasks and Blocking Calls
  4. Structured Concurrency and Error Handling
  5. Ecosystem: Frameworks, Libraries, and Integration

Chapter 7: Choosing Your Concurrency Model

  1. Workload Classification: CPU-Bound, I/O-Bound, Mixed
  2. Performance Characteristics: Comparative Analysis
  3. Complexity and Maintainability Trade-offs
  4. Hybrid Approaches: Combining Models
  5. Decision Framework for Real Projects

Chapter 8: PEP 703 - Removing the Global Interpreter Lock

  1. The Proposal: What PEP 703 Changes and Why
  2. Design Goals and Non-Goals
  3. Reference Counting Without the GIL: Lock-Free and Atomic Operations
  4. The Thread State Model: Per-Thread Interpreters
  5. Compatibility Guarantees: What Stays the Same

Chapter 9: Free-Threading Architecture and Implementation

  1. The No-GIL Build: Configuration and Compilation
  2. Fine-Grained Locking: Per-Object and Per-Resource Locks
  3. Atomic Reference Counting and Memory Ordering
  4. Heap and Arena Allocators Without Global Locks
  5. Interpreter State Isolation and Shared Data

Chapter 10: Running Free-Threading Python Today

  1. Installation: Building and Running No-GIL CPython
  2. Python 3.13+ and the Free-Threading Flag
  3. Checking If Your Environment Is Free-Threaded
  4. Current Performance Characteristics and Benchmarks
  5. Ecosystem Readiness: What Works and What Does Not

Chapter 11: Thread Safety in Free-Threading Python

  1. The Problem: Data Races in Pure Python
  2. Atomic Operations: When Python Guarantees Safety
  3. Compound Operations and Their Vulnerabilities
  4. Defensive Programming Patterns for Thread Safety
  5. Built-in Thread Safety Guarantees

Chapter 12: Migrating to Free-Threading Python

  1. Auditing Your Codebase for Thread Safety
  2. C Extensions: What Breaks and Why
  3. Testing Strategies for Free-Threading Migration
  4. Gradual Migration: Dual-Mode Development
  5. Case Studies: Real Migrating Projects

Chapter 13: Performance of Free-Threading Python

  1. When Free-Threading Helps: True Parallelism in Pure Python
  2. When It Hurts: Lock Contention and Overhead
  3. Scaling with Cores: Benchmarking Methodology and Results
  4. Comparison: Free-Threading vs Multiprocessing vs asyncio
  5. Realistic Expectations for Production Workloads

Chapter 14: Why Rust for Python Extensions

  1. The Problem with C Extensions: Safety, Complexity, Maintenance
  2. What Rust Brings: Memory Safety Without Garbage Collection
  3. Performance Comparison: Rust, C, Cython, Numba
  4. Developer Experience and Ecosystem Maturity
  5. When Rust Is Overkill (and When It Is Not)

Chapter 15: Getting Started with PyO3 and maturin

  1. Installation: Rust, Cargo, PyO3, maturin, and Dependencies
  2. Project Structure: A Python Extension in Rust
  3. Building Your First Extension: From Rust to pip Installable
  4. Calling Rust from Python: Functions, Types, and Modules
  5. Testing Rust Extensions: Rust Tests and Python Tests

Chapter 16: Python and Rust Interoperability

  1. Type Mapping: Python Types and Their Rust Equivalents
  2. Passing Complex Data: Lists, Dicts, Custom Objects
  3. The GIL in PyO3: Python, pyfunction, and gil_refs
  4. Error Handling Across the Boundary: Exceptions and Result
  5. Callbacks: Calling Python from Rust

Chapter 17: Releasing the GIL and Parallel Rust

  1. Understanding GIL Release in PyO3: allow_threads
  2. Thread Pools: rayon, crossbeam, and std::thread
  3. Parallel Algorithms in Rust Extensions
  4. Shared Memory and Concurrent Data Structures
  5. When to Release the GIL and When Not To

Chapter 18: Memory Management at the Boundary

  1. Ownership Semantics: Who Owns What
  2. Zero-Copy Data Transfer: Shared Buffers and Views
  3. Python Object Lifecycle in Rust: Borrowing and References
  4. Avoiding Memory Leaks and Use-After-Free
  5. Large Data Structures and Transfer Patterns

Chapter 19: Packaging and Distribution

  1. maturin Build System: Workflow and Configuration
  2. Wheel Formats: Built vs Source Wheels
  3. Cross-Compilation and Manylinux/Docker Builds
  4. CI/CD: Testing and Publishing Rust Extensions
  5. Versioning, ABI Stability, and Release Strategy

Chapter 20: NumPy and the Vectorization Model

  1. Why NumPy Is Fast: Contiguous Arrays and SIMD
  2. Writing Vectorized Code: Idiomatic Patterns
  3. Under the Hood: NumPy’s C Extensions and Buffers
  4. Performance Gotchas: Copying, Views, and Broadcasting
  5. Modern Alternatives: Numba-accelerated NumPy and More

Chapter 21: Cython - Compiling Python to C

  1. How Cython Works: Typed Python as C Extension Generator
  2. Static Typing: When and How to Annotate
  3. Calling C Libraries from Cython
  4. Performance Analysis: What Cython Speeds Up
  5. Cython vs Rust: Trade-offs and Coexistence

Chapter 22: Numba and JIT Compilation

  1. How Numba Works: LLVM Backend and Compilation Modes
  2. Decorators and Compilation Triggers
  3. GPU Acceleration with Numba CUDA
  4. Performance Characteristics and Limitations
  5. Numba vs Cython vs Rust: Choosing for Your Workload

Chapter 23: PyPy and Alternative Python Implementations

  1. How PyPy Works: Tracing JIT and Meta-Interpreter
  2. Performance Profile: When PyPy Helps and Hurts
  3. Compatibility Issues and C Extension Problems
  4. Other Implementations: GraalPython, MicroPython, Jython
  5. The Future: Will Alternative Implementations Survive Free-Threading?

Chapter 24: Advanced Optimization Techniques

  1. Zero-Copy Data Movement and Buffer Protocols
  2. Serialization Optimization: msgpack, pickle, Arrow
  3. Caching Strategies: Result, Memoization, and Beyond
  4. Memory Layout and Cache Friendliness
  5. Algorithmic Optimization Before Premature Optimization

Chapter 25: Profiling Python Performance

  1. CPU Profiling: cProfile, py-spy, and Visualizers
  2. Memory Profiling: tracemalloc, objgraph, Memory Profiler
  3. Flame Graphs and Call Graphs
  4. Profiling Multithreaded and Multiprocess Code
  5. Profiling Native Extensions and Rust Code

Chapter 26: Designing Reliable Benchmarks

  1. Common Pitfalls: Warm-up, JIT, Caching, and Noise
  2. Microbenchmarks vs Macrobenchmarks
  3. Statistical Rigor: Repetition, Outliers, and Confidence
  4. Comparing Implementations Fairly
  5. Benchmarking Tools: pytest-benchmark, Criterion-Style for Python

Chapter 27: Production Deployment of Performance-Critical Python

  1. Containerization: Docker, Native Extensions, and Build Context
  2. Binary Compatibility and Deployment Environments
  3. Monitoring Performance in Production
  4. CI/CD for Python with Native Dependencies
  5. Incident Response: Debugging Performance Regressions

Chapter 28: The Future of Python Performance

  1. Where Free-Threading Is Heading: Python 3.14 and Beyond
  2. The Rust-Backed Python Ecosystem: Maturing and Expanding
  3. Subinterpreters, Parallel Garbage Collection, and More
  4. Choosing Your Strategy for the Next Five Years
  5. What You Should Do Now

Conclusion

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub