Python Concurrency with asyncio
Matthew Fowler
  • MEAP began September 2020
  • Publication in Summer 2021 (estimated)
  • ISBN 9781617298660
  • 325 pages (estimated)
  • printed in black & white
Learn how to speed up slow Python code with concurrent programming and the cutting-edge asyncio library.

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

The time demands of running code synchronously quickly overload standard Python and slow your programs to a crawl. Python's Asynchronous I/O library asyncio was built to solve these performance problems by making it easy to divide and schedule computational tasks so they can be run independently. asyncio concurrently handles multiple operations without a drop in throughput or responsiveness, making your apps lightning fast and easier to scale.

About the book

Python Concurrency with asyncio teaches you to write concurrent Python code that will boost the speed of your apps and APIs. The book demystifies asynchio's unique single-threaded concurrency model, giving you a behind-the-scenes understanding of the library and its new async/await syntax. Hard-to-grok concurrency topics are broken down into simple flowcharts so you can easily see how your coroutines and tasks are running.

You'll learn to apply asyncio to solve common performance problems, such as batch database jobs, slow web servers, and scaling microservices. All examples you'll build are designed to be usable in the real world, including a clever command line SQL client that can run multiple slow queries at the same time. By the time you're done, you'll even be able to combine asyncio with traditional multiprocessing and multithreading techniques for huge improvements to performance.
Table of Contents detailed table of contents

1. Getting to Know Asyncio

1.1. 1.1 What is asyncio?

1.2. 1.2 What is I/O bound and what is CPU bound?

1.3. 1.3 Understanding Concurrency, Parallelism and Multitasking

1.3.1. 1.3.1 What are concurrency and parallelism?

1.3.2. 1.3.2 The difference between concurrency and parallelism

1.3.3. 1.3.3 What is multitasking?

1.3.4. 1.3.4 The benefits of cooperative multitasking

1.4. 1.4 Understanding processes, threads, multithreading and multiprocessing

1.5. 1.5 Understanding the Global Interpreter Lock

1.5.1. 1.5.1 Is the GIL ever released?

1.5.2. 1.5.2 Asyncio and the GIL

1.6. 1.6 How single threaded concurrency works

1.6.1. 1.6.1 What is a socket?

1.7. 1.7 Understanding the asyncio event loop

1.8. 1.8 Summary

2. Asyncio Basics

2.1. 2.1 Introducing Coroutines

2.1.1. 2.1.1 Creating coroutines with the async keyword

2.1.2. 2.1.2 Pausing execution with the await keyword

2.2. 2.2 Introducing long-running coroutines with sleep

2.3. 2.3 Running concurrently with tasks

2.3.1. 2.3.1 The basics of creating tasks

2.3.2. 2.3.2 Running multiple tasks concurrently

2.4. 2.4 Cancelling tasks and setting timeouts

2.4.1. 2.4.1 Cancelling tasks

2.4.2. 2.4.2 Setting a timeout and canceling with wait_for

2.5. 2.5 Tasks, coroutines, futures and awaitables

2.5.1. 2.5.1 Introducing futures

2.5.2. 2.5.2 The relationship between futures, tasks and coroutines

2.6. 2.6 Measuring coroutine execution time with decorators

2.7. 2.7 The pitfalls of coroutines and tasks

2.7.1. 2.7.1 Running CPU bound code

2.7.2. 2.7.2 Running blocking APIs

2.8. 2.8 Accessing and manually managing the event loop

2.8.1. 2.8.1 Creating an event loop manually

2.8.2. 2.8.2 Accessing the event loop

2.9. 2.9 Using debug mode

2.10. 2.10 Summary

3. A first asyncio application

3.1. 3.1 Working with blocking sockets

3.1.1. 3.1.1 Connecting to a server with telnet

3.1.2. 3.1.2 Reading and writing data to and from a socket

3.1.3. 3.1.3 Allowing multiple connections and the dangers of blocking

3.2. 3.2 Working with non-blocking sockets

3.3. 3.3 Using the selectors module to build a socket event loop

3.4. 3.4 An echo server on the asyncio event loop

3.4.1. 3.4.1 Event loop coroutines for sockets

3.4.2. 3.4.2 Designing an asyncio echo server

3.4.3. 3.4.3 Handling errors in tasks

3.5. 3.5 Shutting down gracefully

3.5.1. 3.5.1 Listening for signals

3.5.2. 3.5.2 Waiting for pending tasks to finish

3.6. 3.6 Summary

4. Concurrent web requests

5. Using non-blocking database drivers

6. Handling CPU-bound work

7. Handling blocking APIs

8. Streaming network connections

9. Web applications with Aiohttp

10. Microservices with Aiohttp

11. Managing subprocesses

12. Async queues

13. Synchronization

14. Advanced Asyncio

What's inside

  • 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

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. He has worked on Python codebases in the machine learning space, as well as led development of a Python-based ecommerce site with tens of millions of users.

placing your order...

Don't refresh or navigate away from the page.
Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
print book $29.99 $59.99 pBook + eBook + liveBook
Additional shipping charges may apply
Python Concurrency with asyncio (print book) added to cart
continue shopping
go to cart

eBook $47.99 3 formats + liveBook
Python Concurrency with asyncio (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.
customers also reading

This book

FREE domestic shipping on three or more pBooks