liveBook · Manning

What you will learn in Python Concurrency with asyncio

by Matthew Fowler

Chapter by chapter

Chapter 1: Getting to know asyncio

1.1 What is asyncio? · 1.2 What is I/O-bound and what is CPU-bound? · 1.3 Understanding concurrency, parallelism, and multitasking · 1.3.1 Concurrency · 1.3.2 Parallelism · 1.3.3 The difference between concurrency and parallelism · 1.3.4 What is multitasking? · 1.3.5 The benefits of cooperative multitasking · 1.4 Understanding processes, threads, multithreading, and multiprocessing · 1.4.1 Process · 1.4.2 Thread · 1.5 Understanding the global interpreter lock · 1.5.1 Is the GIL ever released? · 1.5.2 asyncio and the GIL · 1.6 How single-threaded concurrency works · 1.6.1 What is a socket? · 1.7 How an event loop works · 1.8 Summary

What you learn in chapter 1 →

Chapter 2: asyncio basics

2.1 Introducing coroutines · 2.1.1 Creating coroutines with the async keyword · 2.1.2 Pausing execution with the await keyword · 2.2 Introducing long-running coroutines with sleep · 2.3 Running concurrently with tasks · 2.3.1 The basics of creating tasks · 2.3.2 Running multiple tasks concurrently · 2.4 Canceling tasks and setting timeouts · 2.4.1 Canceling tasks · 2.4.2 Setting a timeout and canceling with wait_for · 2.5 Tasks, coroutines, futures, and awaitables · 2.5.1 Introducing futures · 2.5.2 The relationship between futures, tasks, and coroutines · 2.6 Measuring coroutine execution time with decorators · 2.7 The pitfalls of coroutines and tasks · 2.7.1 Running CPU-bound code · 2.7.2 Running blocking APIs · 2.8 Accessing and manually managing the event loop · 2.8.1 Creating an event loop manually · 2.8.2 Accessing the event loop · 2.9 Using debug mode · 2.9.1 Using asyncio.run · 2.9.2 Using command-line arguments · 2.9.3 Using environment variables · 2.10 Summary

What you learn in chapter 2 →

Chapter 3: A first asyncio application

3.1 Working with blocking sockets · 3.2 Connecting to a server with Telnet · 3.2.1 Reading and writing data to and from a socket · 3.2.2 Allowing multiple connections and the dangers of blocking · 3.3 Working with non-blocking sockets · 3.4 Using the selectors module to build a socket event loop · 3.5 An echo server on the asyncio event loop · 3.5.1 Event loop coroutines for sockets · 3.5.2 Designing an asyncio echo server · 3.5.3 Handling errors in tasks · 3.6 Shutting down gracefully · 3.6.1 Listening for signals · 3.6.2 Waiting for pending tasks to finish · 3.7 Summary

What you learn in chapter 3 →

Chapter 4: Concurrent web requests

4.1 Introducing aiohttp · 4.2 Asynchronous context managers · 4.2.1 Making a web request with aiohttp · 4.2.2 Setting timeouts with aiohttp · 4.3 Running tasks concurrently, revisited · 4.4 Running requests concurrently with gather · 4.4.1 Handling exceptions with gather · 4.5 Processing requests as they complete · 4.5.1 Timeouts with as_completed · 4.6 Finer-grained control with wait · 4.6.1 Waiting for all tasks to complete · 4.6.2 Watching for exceptions · 4.6.3 Processing results as they complete · 4.6.4 Handling timeouts · 4.6.5 Why wrap everything in a task? · 4.7 Summary

What you learn in chapter 4 →

Chapter 5: Non-blocking database drivers

5.1 Introducing asyncpg · 5.2 Connecting to a Postgres database · 5.3 Defining a database schema · 5.4 Executing queries with asyncpg · 5.5 Executing queries concurrently with connection pools · 5.5.1 Inserting random SKUs into the product database · 5.5.2 Creating a connection pool to run queries concurrently · 5.6 Managing transactions with asyncpg · 5.6.1 Nested transactions · 5.6.2 Manually managing transactions · 5.7 Asynchronous generators and streaming result sets · 5.7.1 Introducing asynchronous generators · 5.7.2 Using asynchronous generators with a streaming cursor · 5.8 Summary

What you learn in chapter 5 →

Chapter 6: Handling CPU-bound work

6.1 Introducing the multiprocessing library · 6.2 Using process pools · 6.2.1 Using asynchronous results · 6.3 Using process pool executors with asyncio · 6.3.1 Introducing process pool executors · 6.3.2 Process pool executors with the asyncio event loop · 6.4 Solving a problem with MapReduce using asyncio · 6.4.1 A simple MapReduce example · 6.4.2 The Google Books Ngram dataset · 6.4.3 Mapping and reducing with asyncio · 6.5 Shared data and locks · 6.5.1 Sharing data and race conditions · 6.5.2 Synchronizing with locks · 6.5.3 Sharing data with process pools · 6.6 Multiple processes, multiple event loops · 6.7 Summary

What you learn in chapter 6 →

Chapter 7: Handling blockingwork with threads

7.1 Introducing the threading module · 7.2 Using threads with asyncio · 7.2.1 Introducing the requests library · 7.2.2 Introducing thread pool executors · 7.2.3 Thread pool executors with asyncio · 7.2.4 Default executors · 7.3 Locks, shared data, and deadlocks · 7.3.1 Reentrant locks · 7.3.2 Deadlocks · 7.4 Event loops in separate threads · 7.4.1 Introducing Tkinter · 7.4.2 Building a responsive UI with asyncio and threads · 7.5 Using threads for CPU-bound work · 7.5.1 Multithreading with hashlib · 7.5.2 Multithreading with NumPy · 7.6 Summary

What you learn in chapter 7 →

Chapter 8: Streams

8.1 Introducing streams · 8.2 Transports and protocols · 8.3 Stream readers and stream writers · 8.4 Non-blocking command-line input · 8.4.1 Terminal raw mode and the read coroutine · 8.5 Creating servers · 8.6 Creating a chat server and client · 8.7 Summary

What you learn in chapter 8 →

Chapter 9: Web applications

9.1 Creating a REST API with aiohttp · 9.1.1 What is REST? · 9.1.2 aiohttp server basics · 9.1.3 Connecting to a database and returning results · 9.1.4 Comparing aiohttp with Flask · 9.2 The asynchronous server gateway interface · 9.2.1 How does ASGI compare to WSGI? · 9.3 ASGI with Starlette · 9.3.1 A REST endpoint with Starlette · 9.3.2 WebSockets with Starlette · 9.4 Django asynchronous views · 9.4.1 Running blocking work in an asynchronous view · 9.4.2 Using async code in synchronous views · 9.5 Summary

What you learn in chapter 9 →

Chapter 10: Microservices

10.1 Why microservices? · 10.1.1 Complexity of code · 10.1.2 Scalability · 10.1.3 Team and stack independence · 10.1.4 How can asyncio help? · 10.2 Introducing the backend-for-frontend pattern · 10.3 Implementing the product listing API · 10.3.1 User favorite service · 10.3.2 Implementing the base services · 10.3.3 Implementing the backend-for-frontend service · 10.3.4 Retrying failed requests · 10.3.5 The circuit breaker pattern · 10.4 Summary

What you learn in chapter 10 →

Chapter 11: Synchronization

11.1 Understanding single-threaded concurrency bugs · 11.2 Locks · 11.3 Limiting concurrency with semaphores · 11.3.1 Bounded semaphores · 11.4 Notifying tasks with events · 11.5 Conditions · 11.6 Summary

What you learn in chapter 11 →

Chapter 12: Asynchronous queues

12.1 Asynchronous queue basics · 12.1.1 Queues in web applications · 12.1.2 A web crawler queue · 12.2 Priority queues · 12.3 LIFO queues · 12.4 Summary

What you learn in chapter 12 →

Chapter 13: Managing subprocesses

13.1 Creating a subprocess · 13.1.1 Controlling standard output · 13.1.2 Running subprocesses concurrently · 13.2 Communicating with subprocesses · 13.3 Summary

What you learn in chapter 13 →

Chapter 14: Advanced asyncio

14.1 APIs with coroutines and functions · 14.2 Context variables · 14.3 Forcing an event loop iteration · 14.4 Using different event loop implementations · 14.5 Creating a custom event loop · 14.5.1 Coroutines and generators · 14.5.2 Generator-based coroutines are deprecated · 14.5.3 Custom awaitables · 14.5.4 Using sockets with futures · 14.5.5 A task implementation · 14.5.6 Implementing an event loop · 14.5.7 Implementing a server with a custom event loop · 14.6 Summary

What you learn in chapter 14 →

Concepts explained in this book

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.