Day #96 - AsyncIO in Python

Day #96 - AsyncIO in Python

ยท

3 min read

Introduction

Welcome to my 96th blog post on the Python journey. On day 96, I learned about a way to allow high-performance I/O operations in a concurrent and non-blocking manner. This can be done using AsyncIO Module and its functions. Let's dive into more details and understand these AsyncIo functions in Python.

So let's get started......

Async Tasks

AsyncIO Module

  • AsyncIo is a short form for Asynchronous I/O

  • It is a programming pattern that allows for high-performance I/O operations

  • Using AysncIO can help perform operations in a concurrent and non-blocking manner.

  • We can perform these high-performance operations by making use of the asyncioย  module and asynchronous functions.

Here is the Syntax for creating an asynchronous function using Asyncio Modules -

import asyncio

async def my_async_function():
    # asynchronous code here
    await asyncio.sleep(1)
    return "Hello, Async World!"

async def main():
    result = await my_async_function()
    print(result)

asyncio.run(main())

We can Schedule tasks concurrently as follows:

L = await asyncio.gather(
        my_async_function(),
        my_async_function(),
        my_async_function(),
    )
print(L)

Synchronous and Asynchronous tasks

Benefits of Asynchronous Programming

  • It allows for high-performance and concurrent I/O operations in Python.

  • We can write efficient and scalable code that handles large amounts of data and I/O operations

  • Async IO can act as a essential tool while working with web applications, network services, or data processing pipelines.

Example

import time
import asyncio 
import requests


async def function1():
  print("func 1") 
  URL = "https://wallpaperaccess.in/public/uploads/preview/1920x1200-desktop-background-ultra-hd-wallpaper-wiki-desktop-wallpaper-4k-.jpg"
  response = requests.get(URL)
  open("instagram.ico", "wb").write(response.content)

  return "abc"

async def function2():
  print("func 2") 
  URL = "https://p4.wallpaperbetter.com/wallpaper/490/433/199/nature-2560x1440-tree-snow-wallpaper-preview.jpg"
  response = requests.get(URL)
  open("instagram2.jpg", "wb").write(response.content)
  return "xyz"


async def function3():
  print("func 3")
  URL = "https://c4.wallpaperflare.com/wallpaper/622/676/943/3d-hd-wikipedia-3d-wallpaper-preview.jpg"
  response = requests.get(URL)
  open("instagram3.ico", "wb").write(response.content)

async def main():
  # await function1()
  # await function2()
  # await function3()
  # return 3
  L = await asyncio.gather(
        function1(),
        function2(),
        function3(),
    )
  print(L)
  # task = asyncio.create_task(function1())
  # # await function1()
  # await function2()
  # await function3()

asyncio.run(main())

Output

func 1
func 2
func 3
['abc', 'xyz', None]

Resources Used

You can watch the video of Day#96 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. On day 96, I learned about Asynchronous Programming in Python. This can be used to perform high performance operations without any blockage, concurrently in Python.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #96

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย