Using Asyncio in Python

Book description

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

Publisher resources

View/Submit Errata

Table of contents

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

Product information

  • Title: Using Asyncio in Python
  • Author(s): Caleb Hattingh
  • Release date: February 2020
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492075332