Table Of ContentUsing Asyncio in Python
Understanding Python’s Asynchronous
Programming Features
Caleb Hattingh
BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo
Using Asyncio in Python
by Caleb Hattingh
Copyright © 2020 Tekmoji Pty Ltd. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional
sales department: 800-998-9938 or corporate@oreilly.com.
Acquisitions Editor: Jessica Haberman Indexer: Ellen Troutman-Zaig
Developmental Editor: Corbin Collins Interior Designer: David Futato
Production Editor: Beth Kelly Cover Designer: Karen Montgomery
Copyeditor: Rachel Head Illustrator: Rebecca Demarest
Proofreader: Sharon Wilkey
February 2020: First Edition
Revision History for the First Edition
2020-01-30: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492075332 for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Using Asyncio in Python, the cover
image, and related trade dress are trademarks of O’Reilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publisher’s views.
While the publisher and the author have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the author disclaim all responsibility
for errors or omissions, including without limitation responsibility for damages resulting from the use of
or reliance on this work. Use of the information and instructions contained in this work is at your own
risk. If any code samples or other technology this work contains or describes is subject to open source
licenses or the intellectual property rights of others, it is your responsibility to ensure that your use
thereof complies with such licenses and/or rights.
978-1-492-07533-2
[LSI]
To my partner, Gina: I deeply appreciate all your encouragement and support while
writing this book; it has made all the difference.
—Caleb
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1. Introducing Asyncio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Restaurant of ThreadBots 1
Epilogue 6
What Problem Is Asyncio Trying to Solve? 6
2. The Truth About Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Benefits of Threading 10
Drawbacks of Threading 11
Case Study: Robots and Cutlery 14
3. Asyncio Walk-Through. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Quickstart 22
The Tower of Asyncio 28
Coroutines 31
The New async def Keywords 32
The New await Keyword 34
Event Loop 37
Tasks and Futures 39
Create a Task? Ensure a Future? Make Up Your Mind! 42
Async Context Managers: async with 46
The contextlib Way 47
Async Iterators: async for 50
Simpler Code with Async Generators 53
Async Comprehensions 55
Starting Up and Shutting Down (Gracefully!) 57
What Is the return_exceptions=True for in gather()? 61
v
Signals 63
Waiting for the Executor During Shutdown 68
4. 20 Asyncio Libraries You Aren’t Using (But…Oh, Never Mind). . . . . . . . . . . . . . . . . . . . . 75
Streams (Standard Library) 76
Case Study: A Message Queue 76
Case Study: Improving the Message Queue 84
Twisted 88
The Janus Queue 91
aiohttp 92
Case Study: Hello World 93
Case Study: Scraping the News 93
ØMQ (ZeroMQ) 98
Case Study: Multiple Sockets 99
Case Study: Application Performance Monitoring 102
asyncpg and Sanic 110
Case Study: Cache Invalidation 115
Other Libraries and Resources 126
5. Concluding Thoughts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
A. A Short History of Async Support in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
B. Supplementary Material. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
vi | Table of Contents
Preface
Python 3.4 introduced the asyncio library, and Python 3.5 produced the async and
await keywords to use it palatably. These new additions allow so-called asynchronous
programming.
All of these new features, which I’ll refer to under the single name Asyncio, have been
received by the Python community somewhat warily; a segment of the community
seems to see them as complex and difficult to understand. This view is not limited to
beginners: several high-profile contributors to the Python community have expressed
doubts about the complexity of the Asyncio API in Python, and educators in the
community have expressed concern about how best to teach Asyncio to students.
Most people with a few years’ experience with Python have used threads before, and
even if you haven’t, you are still likely to have experienced blocking. For example, if
you’ve written programs using the wonderful requests library, you will surely have
noticed that your program pauses for a bit while it does requests.get(url); this is
blocking behavior.
For one-off tasks, this is fine; but if you want to fetch ten thousand URLs simultane‐
ously, it’s going to be difficult to use requests. Large-scale concurrency is one big
reason to learn and use Asyncio, but the other big attraction of Asyncio over preemp‐
tive threading is safety: it will be much easier for you to avoid race condition bugs
with Asyncio.
My goal with this book is to give you a basic understanding of why these new features
have been introduced and how to use them in your own projects. More specifically, I
aim to provide the following:
• A critical comparison of asyncio and threading for concurrent network
programming
• An understanding of the new async/await language syntax
• A general overview of the new asyncio standard library features in Python
vii
• Detailed, extended case studies with code, showing how to use a few of the more
popular Asyncio-compatible third-party libraries
We’ll begin with a story that illustrates the shift in thinking that must accompany a
transition from threaded to async programming. Then, we’ll take a look at the
changes that were made in the Python language itself to accommodate async pro‐
gramming. Finally, we’ll explore some of the ways in which these new features can be
used most effectively.
The new Asyncio features are not going to radically change the way you write pro‐
grams. They provide specific tools that make sense only for specific situations; but in
the right situations, asyncio is exceptionally useful. In this book, we’re going to
explore those situations and how you can best approach them by using the new Asyn‐
cio features.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program ele‐
ments such as variable or function names, databases, datatypes, environment
variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.
This element signifies a tip or suggestion.
This element signifies a general note.
viii | Preface