asyncio - How to use Async/Await in Python.

asyncio - How to use Async/Await in Python.

·

5 min read

In this article, we will discuss how we can use async/await in Python.

If you're familiar with JavaScript, you'll notice that we use async, which causes a function to return a promise, and await, which causes a function to wait for a promise.

When we need to fetch some data from the servers, we use async and await to delay the function's execution until the promise is resolved, and in the meantime, the other functions are executed without blocking the other tasks.

This happens because the code is executed asynchronously.

Well, this is not the JavaScript tutorial so we don't dive deep into the working of asynchronous operations in JavaScript.

But we'll learn the meaning of Asynchronous ahead in this article.

Introduction

Just above we encountered the term Asynchronous. What does it mean?

For simple understanding, it just means that not occurring at the same time. It is theoretical meaning. We gonna learn the Practical definition of asynchronous ahead of this article with an example that lets you understand it easily.

Synchronous & Asynchronous Diagram.png

asyncio - Asynchronous IO :

We cannot use async/await syntax directly in the function to make it asynchronous rather we can use a Python package asyncio to achieve the goal.

Before diving into the asyncio module let's understand Asynchronous IO first.

note: I refer to asynchronous IO as async IO and asyncio is a Python package.

Asynchronous IO :

async IO is designed to work on concurrent programming that has received support in Python, from Python 3.4 through Python 3.7 and beyond this.

Let's understand it with the help of an example:

We will understand it by the example of a chess master Judit Polgár.

Judit Polgár, a chess master hosts a chess exhibition in which some amateur players took part and she plays with them. She has two ways of conducting the exhibition: synchronously and asynchronously.

Here are the assumptions:

  • There are 24 opponents.

  • Judit makes each chess move in 5 seconds.

  • Opponents each take 55 seconds to make a move.

  • Games average 30 pair moves (60 moves total).

Synchronous approach:

Judit only plays one game at a time and never moves on until the current game is finished. Each game lasts (55 + 5) 30 = 1800 seconds or 30 minutes. The duration of the exhibition is 24*30 = 720 minutes or 12 hours.

Asynchronous approach:

Judit moves from table to table in this method, making one move at each table. She leaves the table after each move and waits for the opponent to make their next move.

Judit Polgár takes 24*5 = 120 seconds or 2 minutes to complete one move on all 24 games. As a result, the entire exhibition has been reduced to 120*30 = 3600 seconds or just 1 hour.

From the above example, it concludes that async IO tasks that take a long waiting period get blocked and allow other tasks to run during that downtime.

The Source of this example.

Let's move to the next part of this article.

Python's asyncio module :

According to documentation, asyncio is a type of library that helps us to write concurrent code using async/await syntax.

Since it's a module we have to import it.

import asyncio

asyncio works with Coroutines. Coroutines are nothing but a specialized version of Python generator functions.

A coroutine is a function that can suspend its execution before reaching the return and it can indirectly pass the control to another coroutine for some time.

Coroutines declared with the async/await syntax is the preferred way of writing asyncio applications.

Here's the simple use case:

import asyncio

async def func():
    print("Hey....")
    await asyncio.sleep(1)
    print("I am here...")

asyncio.run(func())
  1. async def func() - We used async to make the function asynchronous.

  2. await asyncio.sleep(1) - Here, we used await and used the asyncio.sleep() to delay the execution of the print statement below it.

  3. asyncio.run(func()) - Finally, we are calling the function. You can see that we used asyncio to call the function, if we try to call the function simply as usual then we get a RuntimeError.

  • Note: You must be on Python 3.7 or above.

Until here, you surely get some idea of Asynchronous IO and asyncio use cases.

Let's look at the examples demonstrating the Asynchronous function:

Asynchronous

import asyncio

async def write():
    print("Hey")
    await asyncio.sleep(1)
    print("there")

async def main():
    await asyncio.gather(write(), write(), write())

if __name__ == "__main__":
    import time
    start = time.perf_counter()
    asyncio.run(main())
    elapsed = time.perf_counter() - start
    print(f"File executed in {elapsed:0.2f} seconds")
  1. We created two async functions -

  2. First is the write() function that prints Hey then wait for 1 second and then again prints there.

  3. Second is main() function that executeswrite() function 3 times using asyncio.gather().

  4. And then we wrote a code that calculates the time taken to execute the async functions.

Here's the output:

asyncFirst.png

We can clearly see that the whole code was executed in just 1 second using the asynchronous approach.

What if we execute the same code in a Synchronous way:

Synchronous

import time

def write():
    print("Hey")
    time.sleep(1)
    print("there")

def main():
    for _ in range(3):
        write()

if __name__ == "__main__":
    start = time.perf_counter()
    main()
    elapsed = time.perf_counter() - start
    print(f"File executed in {elapsed:0.2f} seconds")

Here's the output:

async2.png

Here, this code snippet took 3 seconds to execute using the synchronous approach.

Now we are clearly able to see the differences in both approaches and even understand how it's happened.

Conclusion

We can use async/await in Python to make the high-level structured code network for better efficiency.

asyncio is used as a foundation for various Python asynchronous frameworks that provide high-performance networks and web servers, database connection libraries, distributed task queues, etc.


🏆Other articles you might be interested in if you liked this one

Performing pattern matching using match-case statements in Python.

How to use super() function in Python classes?

Different types of inheritances in Python classes.

How to implement __getitem__, __setitem__ and __delitem__ in Python classes?

How to change the string representation of the objects in Python?

What is the difference between sort() and sorted() function in Python?

Public, Protected, and Private access modifiers in Python.


That's all for now

Keep coding✌✌

Did you find this article valuable?

Support Team - GeekPython by becoming a sponsor. Any amount is appreciated!