O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Asyncio in Python 3

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 developers and end-user developers like you. 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 ebook comes in.

Veteran Python developer Caleb Hattingh helps end-user developers gain a basic understanding of Asyncio’s building blocks—enough to get you 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 simple way to support thousands of simultaneous socket connections.

  • Get a critical comparison of Asyncio and threading for concurrent network programming
  • Take an Asyncio walkthrough, including a quickstart guide to help you hit 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

Table of Contents

  1. Preface
  2. 1. Introduction
    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
      1. Discussion
  4. 3. Asyncio Walk-Through
    1. Quickstart
    2. The Tower of Asyncio
      1. Summary
    3. Coroutines
      1. The New async def Keyword
      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. Async Generators: yield Inside async def Functions
    9. Async Comprehensions
    10. Starting Up and Shutting Down (Gracefully!)
      1. What Is the return_exceptions=True for?
      2. Signals
      3. Waiting for the Executor During Shutdown
    11. Testing with asyncio
  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 (APM)
    6. asyncpg and Sanic
      1. Case Study: Cache Invalidation
    7. Other Libraries & Resources
  6. 5. Concluding Thoughts
  7. 6. Appendix
    1. Short History of Async Support in Python
      1. In the Beginning, There Was asyncore
      2. The Path to Native Coroutines
    2. Cutlery Example Using Asyncio
    3. Supplementary Material for News Website Scraper
    4. Supplementary Material for the ZeroMQ Case Study
    5. Database Trigger Handling for the asyncpg Case Study
    6. Supplementary Material for the Sanic Example: aelapsed and aprofiler