Using Asyncio in Python: Understanding Python's Asynchronous Programming Features

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

If you’re among the Python developers put off by asyncio’s complexity, it’s time to take another look. Asyncio is complicated because it aims to solve problems in concurrent network programming for both framework and end-user developers. The features you need to consider are a small subset of the whole asyncio API, but picking out the right features is the tricky part. That’s where this practical book comes in. Veteran Python developer Caleb Hattingh helps you gain a basic understanding of asyncio’s building blocks—enough to get started writing simple event-based programs. You’ll learn why asyncio offers a safer alternative to preemptive multitasking (threading) and how this API provides a simpleway to support thousands of simultaneous socket connections. • Get a critical comparison of asyncio and threading for concurrent network programming • Take an asyncio walk-through, including a quickstart guidefor hitting the ground looping with event-based programming • Learn the difference between asyncio features for end-user developers and those for framework developers • Understand asyncio’s new async/await language syntax, including coroutines and task and future APIs • Get detailed case studies (with code) of some popular asyncio-compatible third-party libraries

Author(s): Caleb Hattingh
Edition: 1
Publisher: O'Reilly Media
Year: 2020

Language: English
Commentary: True PDF
Pages: 166
City: Sebastopol, CA
Tags: Multithreading; Python; Concurrency; Asynchronous Programming; Network Programming; Coroutines; asyncio

Cover
Copyright
Table of Contents
Preface
Conventions Used in This Book
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Introducing Asyncio
The Restaurant of ThreadBots
Epilogue
What Problem Is Asyncio Trying to Solve?
Chapter 2. The Truth About Threads
Benefits of Threading
Drawbacks of Threading
Case Study: Robots and Cutlery
Chapter 3. Asyncio Walk-Through
Quickstart
The Tower of Asyncio
Coroutines
The New async def Keywords
The New await Keyword
Event Loop
Tasks and Futures
Create a Task? Ensure a Future? Make Up Your Mind!
Async Context Managers: async with
The contextlib Way
Async Iterators: async for
Simpler Code with Async Generators
Async Comprehensions
Starting Up and Shutting Down (Gracefully!)
What Is the return_exceptions=True for in gather()?
Signals
Waiting for the Executor During Shutdown
Chapter 4. 20 Asyncio Libraries You Aren’t Using (But…Oh, Never Mind)
Streams (Standard Library)
Case Study: A Message Queue
Case Study: Improving the Message Queue
Twisted
The Janus Queue
aiohttp
Case Study: Hello World
Case Study: Scraping the News
ØMQ (ZeroMQ)
Case Study: Multiple Sockets
Case Study: Application Performance Monitoring
asyncpg and Sanic
Case Study: Cache Invalidation
Other Libraries and Resources
Chapter 5. Concluding Thoughts
Appendix A. A Short History of Async Support in Python
In the Beginning, There Was asyncore
The Path to Native Coroutines
Appendix B. Supplementary Material
Cutlery Example Using Asyncio
Supplementary Material for News Website Scraper
Supplementary Material for the ZeroMQ Case Study
Database Trigger Handling for the asyncpg Case Study
Supplementary Material for the Sanic Example: aelapsed and aprofiler
Index
About the Author
Colophon