Chapter 1 dived into concurrency, looking at how we can achieve it with both processes and threads. We also explored how we could utilize non-blocking I/O and an event loop to achieve concurrency with only one thread. In this chapter, we’ll cover the basics of how to write programs using this single-threaded concurrency model with asyncio. Using the techniques in this chapter, you’ll be able to take long-running operations, such as web requests, database queries, and network connections and execute them in tandem.
We’ll learn more about the coroutine construct and how to use async await syntax to define and run coroutines. We’ll also examine how to run coroutines concurrently by using tasks and examine the time savings we get from running concurrently by creating a reusable timer. Finally, we’ll look at common errors software engineers may make when using asyncio and how to use debug mode to spot these problems.