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 prog…


This content originally appeared on DEV Community and was authored by Santhosh Kumar D

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")

You can easily run these function like below,

while True:
    start_learning()
    start_gaming()

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....

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())    

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


This content originally appeared on DEV Community and was authored by Santhosh Kumar D


Print Share Comment Cite Upload Translate Updates
APA

Santhosh Kumar D | Sciencx (2022-03-17T05:26:09+00:00) What is asyncio and why we need them ? – Python – write concurrent/asynchronous code using the async/await syntax. Retrieved from https://www.scien.cx/2022/03/17/what-is-asyncio-and-why-we-need-them-python-write-concurrent-asynchronous-code-using-the-async-await-syntax/

MLA
" » What is asyncio and why we need them ? – Python – write concurrent/asynchronous code using the async/await syntax." Santhosh Kumar D | Sciencx - Thursday March 17, 2022, https://www.scien.cx/2022/03/17/what-is-asyncio-and-why-we-need-them-python-write-concurrent-asynchronous-code-using-the-async-await-syntax/
HARVARD
Santhosh Kumar D | Sciencx Thursday March 17, 2022 » What is asyncio and why we need them ? – Python – write concurrent/asynchronous code using the async/await syntax., viewed ,<https://www.scien.cx/2022/03/17/what-is-asyncio-and-why-we-need-them-python-write-concurrent-asynchronous-code-using-the-async-await-syntax/>
VANCOUVER
Santhosh Kumar D | Sciencx - » What is asyncio and why we need them ? – Python – write concurrent/asynchronous code using the async/await syntax. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/17/what-is-asyncio-and-why-we-need-them-python-write-concurrent-asynchronous-code-using-the-async-await-syntax/
CHICAGO
" » What is asyncio and why we need them ? – Python – write concurrent/asynchronous code using the async/await syntax." Santhosh Kumar D | Sciencx - Accessed . https://www.scien.cx/2022/03/17/what-is-asyncio-and-why-we-need-them-python-write-concurrent-asynchronous-code-using-the-async-await-syntax/
IEEE
" » What is asyncio and why we need them ? – Python – write concurrent/asynchronous code using the async/await syntax." Santhosh Kumar D | Sciencx [Online]. Available: https://www.scien.cx/2022/03/17/what-is-asyncio-and-why-we-need-them-python-write-concurrent-asynchronous-code-using-the-async-await-syntax/. [Accessed: ]
rf:citation
» What is asyncio and why we need them ? – Python – write concurrent/asynchronous code using the async/await syntax | Santhosh Kumar D | Sciencx | https://www.scien.cx/2022/03/17/what-is-asyncio-and-why-we-need-them-python-write-concurrent-asynchronous-code-using-the-async-await-syntax/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.