DEV Community 👩‍💻👨‍💻

Cover image for What is asyncio and why we need them ? - Python - write concurrent/asynchronous code using the async/await syntax
Santhosh Kumar D
Santhosh Kumar D

Posted on

What is asyncio and why we need them ? - Python - write concurrent/asynchronous code using the async/await syntax

TL;DR - asyncio let's your write code which switches between different tasks while maintaining the state of each task to continue further
Target Audience - Beginner to mid-level
Assumption - This post assumes that you have some low-level knowledge programming in any language.
Disclaimer - The post is written with my limited personal knowledge and experience, there could be areas of improvement. Feel free to comment on them.

-
Let's begin!!

All the code you might have seen or written so far (if you're new to async) would be called synchronize, why? Because it runs the lines of code one after the other or in other words if you have two functions it could only run in a sequence.
What we'll do in this blog post is try to break this, difference languages use different methods of doing things. But for this we'll use Python3.7+ and asyncio

Let's look at a real life example of this, 🎮📚
Assume you're studying for your semester exam, but you also have a gaming tournament coming up so you want to practice of it. So what you do? Learn for 8 hours and practice of 4 hours a day. You keep doing this till you complete your exam and the tournament.

This is the same we're trying to achieve with asynchronous programming through asyncio in Python.

Let's start off with the simple one,

def start_learning():
    print("I'm learning now")

def start_gaming():
    print("I'm playing Rainbow Siege")
Enter fullscreen mode Exit fullscreen mode

You can easily run these function like below,

while True:
    start_learning()
    start_gaming()
Enter fullscreen mode Exit fullscreen mode

Ouput:

I'm learning now
I'm playing Rainbow Siege
I'm learning now
I'm playing Rainbow Siege
I'm learning now
I'm playing Rainbow Siege
and so on....
Enter fullscreen mode Exit fullscreen mode

Let's see how things look with asyncio
We want to run both at the same time and let the tasks decide when to switch. Let's say a chapter is easier to complete than others, then we can switch to gaming instead of waiting for the remaining hours to end.

async def start_learning():
    print("I'm learning now")
    # replace the below line with whatever the task that you want 
    # to perform
    await asyncio.sleep(8)

async def start_gaming():
    print("I'm playing Rainbow Siege")
    # replace the below line with whatever the task that you want 
    # to perform
    await asyncio.sleep(4)

async def main():
    await asyncio.gather(start_learning(), start_gaming())

asyncio.run(main())    
Enter fullscreen mode Exit fullscreen mode

This is a pretty basic example some would need to understand asynchronous coding.
Feel free to drop in comments if you have questions on the code or if something can be improved.
Happy to learn!!

Let's connect 🤝

LinkedIn

Instagram

Twitter

Top comments (0)

🌚 Life is too short to browse without dark mode