concept await in category asyncio

appears as: wait
Python Concurrency with asyncio

This is an excerpt from Manning's book Python Concurrency with asyncio.

In Python version 3.4, asyncio was first introduced with decorators alongside generator yield from syntax to define coroutines. A coroutine is a method that can be paused when we have a long-running task and then resumed when that task is finished. In Python version 3.5, the language got first-class support for coroutines and asynchronous programming when the keywords async and await were explicitly added to the language. This syntax, common in other programming languages such as C# and Javascript, allows us to make our asynchronous code look like it is run synchronously. This makes asynchronous code easy to read and understand as it looks like the sequential flow most software engineers are familiar with. Asyncio is a library to execute these coroutines in an asynchronous fashion using a concurrency model known as a single-threaded event loop.

While the name of asyncio may make us think that this library is only good for I/O operations, it has functionality to handle other types of operations as well by interoperating with multithreading and multiprocessing. With this interoperability we can use async and await syntax with threads and processes making these workflows easier to understand. This means this library is not only good for I/O based concurrency but can also be used with code that is CPU intensive.

We’ll learn more about a construct called coroutines 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 take a look at common errors software engineers make when using asyncio and how to use debug mode to spot these problems.

Creating a task is achieved by using the asyncio.create_task function. When we call this function, we give it a coroutine to run and it returns a task object instantly. Once we have a task object, we can put it in an await expression which will extract the return value once it is complete.

Listing 2.8 creating a task
import asyncio
from util import delay
 
 
async def main():
    sleep_for_three = asyncio.create_task(delay(3))
    print(type(sleep_for_three))
    result = await sleep_for_three
    print(result)
 
asyncio.run(main())

In listing 2.8 we create a task that will take three seconds to complete. We also print out the type of the task, in this case, <class '_asyncio.Task'>, to show that it is different from a coroutine. One other thing to note here is that our print statement gets executed immediately after we create the task. If we had simply used await on the delay coroutine we would have waited three seconds before outputting the message. Once we’ve printed our message, we apply an await expression to the task sleep_for_three. This will suspend our main coroutine until we have a result from our task.

It is important to know that we should usually use an await keyword on our tasks at some point in our application. In listing 2.8 if we did not use await our task would get scheduled to run but would almost immediately get stopped and ‘cleaned up’ when asyncio.run shuts down the event loop. Using await on our tasks in our application also has implications for how exceptions are handled, which we’ll take a look at in the next chapter.