Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Matthew Fowler

Learn how to speed up slow Python code with concurrent programming and the cutting-edge asyncio library. • Use coroutines and tasks alongside async/await syntax to run code concurrently • Build web APIs and make concurrency web requests with aiohttp • Run thousands of SQL queries concurrently • Create a map-reduce job that can process gigabytes of data concurrently • Use threading with asyncio to mix blocking code with asyncio code Python is flexible, versatile, and easy to learn. It can also be very slow compared to lower-level languages. Python Concurrency with asyncio teaches you how to boost Python's performance by applying a variety of concurrency techniques. You'll learn how the complex-but-powerful asyncio library can achieve concurrency with just a single thread and use asyncio's APIs to run multiple web requests and database queries simultaneously. The book covers using asyncio with the entire Python concurrency landscape, including multiprocessing and multithreading. About the technology It’s easy to overload standard Python and watch your programs slow to a crawl. The asyncio library was built to solve these problems by making it easy to divide and schedule tasks. It seamlessly handles multiple operations concurrently, leading to apps that are lightning fast and scalable. About the book Python Concurrency with asyncio introduces asynchronous, parallel, and concurrent programming through hands-on Python examples. Hard-to-grok concurrency topics are broken down into simple flowcharts that make it easy to see how your tasks are running. You’ll learn how to overcome the limitations of Python using asyncio to speed up slow web servers and microservices. You’ll even combine asyncio with traditional multiprocessing techniques for huge improvements to performance. What's inside • Build web APIs and make concurrency web requests with aiohttp • Run thousands of SQL queries concurrently • Create a map-reduce job that can process gigabytes of data concurrently • Use

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# Python Concurrency with asyncio — Reading Guide ## 【One-Line Pitch】 A hands-on, practical guide to mastering Python's asyncio library for building concurrent applications — from coroutines and event loops to web APIs, database queries, and even combining asyncio with multiprocessing for serious performance gains. Ideal for Python developers who've hit performance walls with slow, blocking code and want to understand concurrency without drowning in theory. --- ## 【Book Arc】 - **Opening (~0%–9%)**: Establishes the fundamental concepts of concurrency, parallelism, and multitasking — distinguishing preemptive (OS-controlled) from cooperative (application-controlled) multitasking, and explaining why asyncio's cooperative model is less resource-intensive and offers finer control over when tasks pause. - **Early (~9%–28%)**: Introduces the core asyncio building blocks — coroutines, the `async`/`await` syntax, tasks, futures, and the `Awaitable` inheritance hierarchy. Covers `asyncio.run()` as the main entry point, the event loop's role, and the critical lesson that blocking libraries (like `requests`) defeat asyncio's purpose, requiring non-blocking alternatives (like `aiohttp`). - **Middle (~28%–47%)**: Builds a first real asyncio application — a socket-based echo server — walking through non-blocking sockets, the `select` mechanism, and translating low-level socket code into idiomatic async/await. Covers event loop management, signal handlers for graceful shutdown, and waiting for pending tasks to complete. - **Late (~47%–60%)**: Moves into practical patterns like asynchronous context managers for resource cleanup, and introduces the `aiohttp` library for building web APIs and making concurrent web requests — the first major real-world application of the concepts learned. - **Ending (~60%–100%)**: Expands into advanced territory — running thousands of concurrent SQL queries, building a map-reduce job for processing gigabytes of data, and combining asyncio with threading and multiprocessing to handle blocking code within async applications. --- ## 【Key Takeaways】 - **Concurrency ≠ parallelism** (Opening): Concurrency means multiple tasks making progress, but only one executes at a time in asyncio's single-threaded model. Understanding this distinction prevents unrealistic performance expectations and guides architectural decisions. - **Cooperative multitasking is asyncio's foundation** (Opening): Unlike OS-preemptive multitasking, asyncio lets you explicitly mark pause points in code, reducing context-switch overhead and giving you granular control over when tasks yield — the key to its efficiency. - **`asyncio.run()` is your single entry point** (Early): It creates the event loop, runs one main coroutine, and handles cleanup. That main coroutine should launch all other tasks — a pattern you'll use in nearly every asyncio application. - **`await` pauses, doesn't block** (Early): The `await` keyword suspends the current coroutine until the awaited operation completes, letting the event loop run other tasks. This is the mechanism that enables concurrency with a single thread. - **Blocking libraries kill asyncio** (Early): If a library doesn't return coroutines (like `requests`), it blocks the event loop, destroying all concurrency benefits. You need non-blocking alternatives like `aiohttp`, or must offload blocking calls to thread pools. - **The `Awaitable` hierarchy unifies everything** (Early): Coroutines, futures, and tasks all inherit from `Awaitable` via the `__await__` method — understanding this inheritance diagram clarifies how these types relate and when to use each. - **Non-blocking sockets are the raw material** (Middle): Setting `setblocking(False)` on sockets makes I/O operations return instantly, allowing the event loop to monitor multiple connections — the foundation of building scalable network servers. - **Graceful shutdown requires signal handling** (Middle): Using `loop.add_signal_handler()` to catch signals like SIGINT and cancel running tasks prevents orphaned operations and ensures clean application termination. --- ## 【Reading Tips】 - **Skim the opening theory chapters (0–9%)** if you're already comfortable with concurrency concepts — the key distinction to internalize is cooperative vs. preemptive multitasking, which frames everything that follows. - **Deep-read the early chapters (9–28%)** on coroutines, tasks, and the event loop — these are the building blocks you'll use constantly. Pay special attention to the blocking-vs-non-blocking library discussion; it will save you hours of debugging later. - **Work through the echo server example (Middle)** hands-on — it's the book's first complete application and ties together sockets, event loops, and async/await in a way that makes the machinery click. - **The later chapters on aiohttp, SQL concurrency, and map-reduce** are best read as reference material — skim to understand the patterns, then return when you need to implement similar functionality in your own projects. - **Watch for the flowcharts and diagrams** — the book uses them extensively to visualize task scheduling and event loop behavior, which are much easier to grasp visually than through text alone. --- ## 【Coverage Limits】 This guide covers the book's progression from asyncio fundamentals through practical applications, but the excerpts don't include the final chapters' details on combining asyncio with multiprocessing/threading or the map-reduce implementation specifics. The later sections on aiohttp web APIs and concurrent SQL queries are mentioned but not deeply excerpted. --- ##
Excerpt 1
ild web APIs and make concurrency web requests with aiohttp • Run thousands of SQL queries concurrently • Create a map-reduce job that can process gigabytes...
View in text
Excerpt 2
routine that asyncio.run executes will create and run other coroutines that will allow us to utilize the concurrent nature of asyncio. 2.1.2 Pausing executio...
View in text
Excerpt 3
nt loop. However, there may be cases in which we don’t want the functionality that asyncio.run provides. As an example, we may want to execute custom logic t...
View in text
Excerpt 4
a wait_for and then await those wrapped tasks. Those tasks will then throw a TimeoutError once the timeout has passed and we can terminate our application. O...
View in text
Excerpt 5
pleted call has taken; if it takes longer than the timeout, each awaitable in the iterator will throw a TimeoutException when we await it. To illustrate this...
View in text
Excerpt 6
e any changes in our transaction permanent in the database. Up until now we have been running our queries in a way that pulls all query results into memory a...
View in text
Excerpt 7
elize our map operations. We’ll create a process pool, par- tition our data into chunks, and for each partition run map_frequencies in a resource (“worker”)...
View in text
Excerpt 8
common paradigm in web servers such as Apache and is known as a thread-per-connection model. Let’s give this idea a try by waiting for connections in our mai...
View in text
Tags
AI categories
Programming LanguagePythonBackend
ISBN: 1617298662
Publish Year: 2022
Language: English
Pages: 378
File Format: PDF
File Size: 6.1 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…