Python Concurrency with asyncio

Book description

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. Th e 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 threading with asyncio to mix blocking code with asyncio code


About the Reader
For intermediate Python programmers. No previous experience of concurrency required.

About the Author
Matthew Fowler has over 15 years of software engineering experience in roles from architect to engineering director.

Quotes
A clear and practical guide to using asyncio to solve real-world concurrency problems.
- Didier Garcia, Software Sushi

Well organized and written…effectively guides the reader through a variety of impactful concurrency techniques.
- Dan Sheikh, BCG Digital Ventures

If you want to efficiently take your Python skills to the next level, look no further. This is the book for you.
- Eli Mayost, IBM

Provides a lot of insights into one of the trickier aspects of Python programming, namely asyncio.
- Peter White, Charles Sturt University

Table of contents

  1. inside front cover
  2. Python Concurrency with asyncio
  3. Copyright
  4. dedication
  5. contents
  6. front matter
    1. preface
    2. acknowledgments
    3. about this book
    4. Who should read this book?
    5. How this book is organized: A road map
    6. About the code
    7. liveBook discussion forum
    8. about the author
    9. about the cover illustration
  7. 1 Getting to know asyncio
    1. 1.1 What is asyncio?
    2. 1.2 What is I/O-bound and what is CPU-bound?
    3. 1.3 Understanding concurrency, parallelism, and multitasking
      1. 1.3.1 Concurrency
      2. 1.3.2 Parallelism
      3. 1.3.3 The difference between concurrency and parallelism
      4. 1.3.4 What is multitasking?
      5. 1.3.5 The benefits of cooperative multitasking
    4. 1.4 Understanding processes, threads, multithreading, and multiprocessing
      1. 1.4.1 Process
      2. 1.4.2 Thread
    5. 1.5 Understanding the global interpreter lock
      1. 1.5.1 Is the GIL ever released?
      2. 1.5.2 asyncio and the GIL
    6. 1.6 How single-threaded concurrency works
      1. 1.6.1 What is a socket?
    7. 1.7 How an event loop works
    8. Summary
  8. 2 asyncio basics
    1. 2.1 Introducing coroutines
      1. 2.1.1 Creating coroutines with the async keyword
      2. 2.1.2 Pausing execution with the await keyword
    2. 2.2 Introducing long-running coroutines with sleep
    3. 2.3 Running concurrently with tasks
      1. 2.3.1 The basics of creating tasks
      2. 2.3.2 Running multiple tasks concurrently
    4. 2.4 Canceling tasks and setting timeouts
      1. 2.4.1 Canceling tasks
      2. 2.4.2 Setting a timeout and canceling with wait_for
    5. 2.5 Tasks, coroutines, futures, and awaitables
      1. 2.5.1 Introducing futures
      2. 2.5.2 The relationship between futures, tasks, and coroutines
    6. 2.6 Measuring coroutine execution time with decorators
    7. 2.7 The pitfalls of coroutines and tasks
      1. 2.7.1 Running CPU-bound code
      2. 2.7.2 Running blocking APIs
    8. 2.8 Accessing and manually managing the event loop
      1. 2.8.1 Creating an event loop manually
      2. 2.8.2 Accessing the event loop
    9. 2.9 Using debug mode
      1. 2.9.1 Using asyncio.run
      2. 2.9.2 Using command-line arguments
      3. 2.9.3 Using environment variables
    10. Summary
  9. 3 A first asyncio application
    1. 3.1 Working with blocking sockets
    2. 3.2 Connecting to a server with Telnet
      1. 3.2.1 Reading and writing data to and from a socket
      2. 3.2.2 Allowing multiple connections and the dangers of blocking
    3. 3.3 Working with non-blocking sockets
    4. 3.4 Using the selectors module to build a socket event loop
    5. 3.5 An echo server on the asyncio event loop
      1. 3.5.1 Event loop coroutines for sockets
      2. 3.5.2 Designing an asyncio echo server
      3. 3.5.3 Handling errors in tasks
    6. 3.6 Shutting down gracefully
      1. 3.6.1 Listening for signals
      2. 3.6.2 Waiting for pending tasks to finish
    7. Summary
  10. 4 Concurrent web requests
    1. 4.1 Introducing aiohttp
    2. 4.2 Asynchronous context managers
      1. 4.2.1 Making a web request with aiohttp
      2. 4.2.2 Setting timeouts with aiohttp
    3. 4.3 Running tasks concurrently, revisited
    4. 4.4 Running requests concurrently with gather
      1. 4.4.1 Handling exceptions with gather
    5. 4.5 Processing requests as they complete
      1. 4.5.1 Timeouts with as_completed
    6. 4.6 Finer-grained control with wait
      1. 4.6.1 Waiting for all tasks to complete
      2. 4.6.2 Watching for exceptions
      3. 4.6.3 Processing results as they complete
      4. 4.6.4 Handling timeouts
      5. 4.6.5 Why wrap everything in a task?
    7. Summary
  11. 5 Non-blocking database drivers
    1. 5.1 Introducing asyncpg
    2. 5.2 Connecting to a Postgres database
    3. 5.3 Defining a database schema
    4. 5.4 Executing queries with asyncpg
    5. 5.5 Executing queries concurrently with connection pools
      1. 5.5.1 Inserting random SKUs into the product database
      2. 5.5.2 Creating a connection pool to run queries concurrently
    6. 5.6 Managing transactions with asyncpg
      1. 5.6.1 Nested transactions
      2. 5.6.2 Manually managing transactions
    7. 5.7 Asynchronous generators and streaming result sets
      1. 5.7.1 Introducing asynchronous generators
      2. 5.7.2 Using asynchronous generators with a streaming cursor
    8. Summary
  12. 6 Handling CPU-bound work
    1. 6.1 Introducing the multiprocessing library
    2. 6.2 Using process pools
      1. 6.2.1 Using asynchronous results
    3. 6.3 Using process pool executors with asyncio
      1. 6.3.1 Introducing process pool executors
      2. 6.3.2 Process pool executors with the asyncio event loop
    4. 6.4 Solving a problem with MapReduce using asyncio
      1. 6.4.1 A simple MapReduce example
      2. 6.4.2 The Google Books Ngram dataset
      3. 6.4.3 Mapping and reducing with asyncio
    5. 6.5 Shared data and locks
      1. 6.5.1 Sharing data and race conditions
      2. 6.5.2 Synchronizing with locks
      3. 6.5.3 Sharing data with process pools
    6. 6.6 Multiple processes, multiple event loops
    7. Summary
  13. 7 Handling blockingwork with threads
    1. 7.1 Introducing the threading module
    2. 7.2 Using threads with asyncio
      1. 7.2.1 Introducing the requests library
      2. 7.2.2 Introducing thread pool executors
      3. 7.2.3 Thread pool executors with asyncio
      4. 7.2.4 Default executors
    3. 7.3 Locks, shared data, and deadlocks
      1. 7.3.1 Reentrant locks
      2. 7.3.2 Deadlocks
    4. 7.4 Event loops in separate threads
      1. 7.4.1 Introducing Tkinter
      2. 7.4.2 Building a responsive UI with asyncio and threads
    5. 7.5 Using threads for CPU-bound work
      1. 7.5.1 Multithreading with hashlib
      2. 7.5.2 Multithreading with NumPy
    6. Summary
  14. 8 Streams
    1. 8.1 Introducing streams
    2. 8.2 Transports and protocols
    3. 8.3 Stream readers and stream writers
    4. 8.4 Non-blocking command-line input
      1. 8.4.1 Terminal raw mode and the read coroutine
    5. 8.5 Creating servers
    6. 8.6 Creating a chat server and client
    7. Summary
  15. 9 Web applications
    1. 9.1 Creating a REST API with aiohttp
      1. 9.1.1 What is REST?
      2. 9.1.2 aiohttp server basics
      3. 9.1.3 Connecting to a database and returning results
      4. 9.1.4 Comparing aiohttp with Flask
    2. 9.2 The asynchronous server gateway interface
      1. 9.2.1 How does ASGI compare to WSGI?
    3. 9.3 ASGI with Starlette
      1. 9.3.1 A REST endpoint with Starlette
      2. 9.3.2 WebSockets with Starlette
    4. 9.4 Django asynchronous views
      1. 9.4.1 Running blocking work in an asynchronous view
      2. 9.4.2 Using async code in synchronous views
    5. Summary
  16. 10 Microservices
    1. 10.1 Why microservices?
      1. 10.1.1 Complexity of code
      2. 10.1.2 Scalability
      3. 10.1.3 Team and stack independence
      4. 10.1.4 How can asyncio help?
    2. 10.2 Introducing the backend-for-frontend pattern
    3. 10.3 Implementing the product listing API
      1. 10.3.1 User favorite service
      2. 10.3.2 Implementing the base services
      3. 10.3.3 Implementing the backend-for-frontend service
      4. 10.3.4 Retrying failed requests
      5. 10.3.5 The circuit breaker pattern
    4. Summary
  17. 11 Synchronization
    1. 11.1 Understanding single-threaded concurrency bugs
    2. 11.2 Locks
    3. 11.3 Limiting concurrency with semaphores
      1. 11.3.1 Bounded semaphores
    4. 11.4 Notifying tasks with events
    5. 11.5 Conditions
    6. Summary
  18. 12 Asynchronous queues
    1. 12.1 Asynchronous queue basics
      1. 12.1.1 Queues in web applications
      2. 12.1.2 A web crawler queue
    2. 12.2 Priority queues
    3. 12.3 LIFO queues
    4. Summary
  19. 13 Managing subprocesses
    1. 13.1 Creating a subprocess
      1. 13.1.1 Controlling standard output
      2. 13.1.2 Running subprocesses concurrently
    2. 13.2 Communicating with subprocesses
    3. Summary
  20. 14 Advanced asyncio
    1. 14.1 APIs with coroutines and functions
    2. 14.2 Context variables
    3. 14.3 Forcing an event loop iteration
    4. 14.4 Using different event loop implementations
    5. 14.5 Creating a custom event loop
      1. 14.5.1 Coroutines and generators
      2. 14.5.2 Generator-based coroutines are deprecated
      3. 14.5.3 Custom awaitables
      4. 14.5.4 Using sockets with futures
      5. 14.5.5 A task implementation
      6. 14.5.6 Implementing an event loop
      7. 14.5.7 Implementing a server with a custom event loop
    6. Summary
  21. index
  22. inside back cover

Product information

  • Title: Python Concurrency with asyncio
  • Author(s): Matthew Fowler
  • Release date: February 2022
  • Publisher(s): Manning Publications
  • ISBN: 9781617298660