1 / 11
Learn concurrency in Python the practical way and build a real concurrent application: concurrency vs parallelism, the GIL and free-threaded builds, threads and thread safety, locks and queues, concurrent.futures, multiprocessing and shared state, asyncio from first principles, TaskGroup and…
Threads, Processes, asyncio, and Structured Concurrency for Faster, Responsive Programs
Read before you buy: the PDF here is a free sample — the first 20 pages of the 517-page book (payments, sales and reviews on this demo site are sample data). Get the full e-book at thenexbooks on Gumroad.
Make your Python programs stop waiting — and know exactly which concurrency tool to use, and why.
Most programs spend most of their time waiting: for web servers, databases, files, and users. Others crunch numbers on one core while the rest of the machine sits idle. Python gives you three serious answers — threads, processes, and asyncio — and each one comes with its own rules, traps, and folklore. This book teaches all three from the ground up and shows you how to choose between them with measurements instead of guesses.
Every technique is demonstrated on one running application, every failure is shown before it is fixed — the race condition, the frozen event loop, the orphaned task, the deadlock — and every example was run on a real machine with real output.
What's inside
- Concurrency vs parallelism, I/O-bound vs CPU-bound work, and how to measure before you optimize
- What the GIL really does, when it is released, and what the free-threaded builds of recent Python versions change (and what they do not)
- Threads done right: starting and joining, race conditions, locks, deadlocks, queues, events, conditions, and semaphores
- Thread pools and process pools with concurrent.futures, multiprocessing, the spawn start method, pickling costs, and shared memory
- asyncio from first principles: coroutines, the event loop, tasks, gather, as_completed, and task lifetimes
- Structured concurrency with TaskGroup, exception groups and except*, timeouts with asyncio.timeout, and cancellation that cleans up properly
- Async generators, async context managers, and async iteration
- Real async I/O with httpx and aiohttp: shared clients, connection pools, semaphores, token-bucket rate limiting, and being a polite client
- Resilience: retries with exponential backoff, partial failure, and circuit breakers
- Producer/consumer pipelines with asyncio.Queue and backpressure, plus a small async server of your own
- Mixing worlds: asyncio.to_thread, run_in_executor, calling async code from sync code, and async patterns for databases and files
- Testing threaded and async code with pytest, pytest-asyncio, local test servers, and httpx.MockTransport
- Debugging with asyncio debug mode, faulthandler, and logging; profiling with pyinstrument
- A decision guide for choosing the right model, production patterns with graceful shutdown, and a complete capstone
One running example, start to finish. You build Gullwing, a fictional price tracker that polls several online stores, finds the best in-stock price for each product, and sends alerts when a price hits a target. The stores are served by a small fake store server that runs on your own computer — with deliberately slow responses, missing products, a flaky store, strict rate limits, and one very slow store for timeout experiments — so every experiment is repeatable and nothing touches the internet. Polling one request at a time takes about 4.5 seconds by design; you will watch that number fall chapter by chapter, and finish with a tested, structured application that runs unattended and shuts down cleanly.
Current and honest. The book targets Python 3.12 and later and clearly labels features added in Python 3.13 and 3.14, including free-threaded builds and subinterpreters. It contains no invented benchmarks or statistics: timings are real measurements from one machine, presented as illustrations rather than promises. Every chapter includes runnable code with real output, Tips, Notes, Warnings, exercises, and Key Takeaways, and the appendices give you a cheat sheet, templates and reference tables, a production checklist, and a glossary.
Format: PDF, 517 pages (A4), linked table of contents and bookmarks. English. First Edition, 2026. Published by thenexbooks. Requires basic Python knowledge (functions, classes, modules, pip). This book is independent and is not affiliated with, sponsored by, or endorsed by the Python Software Foundation.
Table of Contents
Part I — Foundations
- Ch 1 · Why Concurrency: Waiting, Working, and the Gullwing Price Tracker
- Ch 2 · Concurrency vs. Parallelism: I/O-Bound, CPU-Bound, and Measuring First
- Ch 3 · Inside CPython: The GIL, Free-Threaded Builds, and What They Mean for You
- Ch 4 · Threads with the threading Module: Starting, Joining, and Daemon Threads
- Ch 5 · Thread Safety: Race Conditions, Locks, and the Atomicity Myth
Part II — Threads and Processes
- Ch 6 · Coordinating Threads: Queues, Events, Conditions, and Semaphores
- Ch 7 · concurrent.futures: Executors, Futures, and Thread Pools Done Right
- Ch 8 · Multiprocessing: Processes, Pools, and the Spawn Start Method
- Ch 9 · Sharing State Between Processes: Queues, Pipes, Shared Memory, and Managers
- Ch 10 · Threads or Processes? CPU Work, Pickling Costs, and Hybrid Designs
Part III — asyncio Fundamentals
- Ch 11 · asyncio from First Principles: Coroutines, await, and the Event Loop
- Ch 12 · Tasks: Scheduling, gather, as_completed, and Task Lifetimes
- Ch 13 · Structured Concurrency with TaskGroup and Exception Groups
- Ch 14 · Timeouts and Cancellation: asyncio.timeout, CancelledError, and Cleanup
- Ch 15 · Async Iteration and Context: Async Generators, async for, and async with
Part IV — Async I/O in Practice
- Ch 16 · Async HTTP with httpx and aiohttp: Clients, Sessions, and Connection Pools
- Ch 17 · Limiting Concurrency: Semaphores, Rate Limiters, and Polite Clients
- Ch 18 · Resilience: Retries, Backoff, Partial Failure, and Circuit Breakers
- Ch 19 · Producer/Consumer Pipelines with asyncio.Queue
- Ch 20 · Async Servers and Streams: Building the Fake Store with aiohttp
Part V — Mixing Worlds
- Ch 21 · Bridging Sync and Async: to_thread, run_in_executor, and Calling Async from Sync
- Ch 22 · Async Data Access: Databases, Files, and Blocking Libraries
- Ch 23 · Testing Concurrent and Async Code with pytest and pytest-asyncio
- Ch 24 · Debugging Concurrency: Debug Mode, Deadlocks, Leaks, and Logging
Part VI — Performance and Production
- Ch 25 · Profiling and Measuring Concurrent Programs
- Ch 26 · Choosing the Right Model: A Decision Guide and What Comes Next
- Ch 27 · Production Patterns: Configuration, Graceful Shutdown, and Observability
- Ch 28 · Capstone: Shipping Gullwing, a Concurrent Price Tracker
Appendices
A · Concurrency and asyncio Cheat Sheet
B · Templates and Reference
C · Production Checklist
D · Glossary
Book details
| Detail | |
|---|---|
| Pages | 517 |
| Format | PDF (A4) |
| Language | English |
| Level | Intermediate |
| Edition | 1st Edition (2026) |
| Download on this site | Free sample, first 20 pages (PDF) |
| Publisher | thenexbooks |
Files you get
No reviews yet. Only buyers can review.
More from thenexbooks