Async Function Using Schedule Library

Async Function Using Schedule Library

The async functions are very popular in Python, which is always used in asynchronous programming. The Async function is mainly used in operations where we need to wait for a certain period of time. In simple words, the async function is used to execute non-blocking programs in Python. These async functions can be implemented in Python using the schedule library. In this article, we are going to see how these async functions will be executed in Python.

Schedule Library in Python

The schedule library in Python is a very simple and easy-to-use library. This library contains different built-in functions and modules that help manage tasks according to schedule or time. Therefore, this library is mainly used for asynchronous programming. This library provides a very convenient way to schedule the task according to the code. The tasks can be managed according to day, week, month, or year. In this way, we can always write non-blocking code in Python.

The combination of asyncio and the schedule library always helps to execute the asynchronous tasks along with the scheduled tasks. Let’s see the simple code to combine both libraries in one frame.

Schedule Library With Async Function

To use the schedule library with the async function, we need to install the asyncio and schedule libraries first. Let’s see how to install these libraries in Python.

pip install schedule
pip install asyncio
Installation Of Libraries For Async Function
Installation Of Libraries For Async Function

Now let’s execute the code using both the libraries, where scheduled tasks and asynchronous tasks are arranged in a non-blocking way.

import schedule
import time
import threading
import asyncio

async def async_task():
    print("Async task is running")
def run_scheduler():
    while True:
        schedule.run_pending()
        time.sleep(1)
async def main():
    schedule.every(5).seconds.do(async_task)
    scheduler_thread = threading.Thread(target=run_scheduler)
    scheduler_thread.start()   
    while True:
        await asyncio.sleep(1)
   

In this code, we are importing different modules of Python, like schedule, time, threading, and asyncio to implement the async function with regular tasks. In this, we have used a schedule.every() function to define the target job. The async task will run according to time intervals. The result will be printed for the async function.

Async Function With Schedule Library
Async Function With Schedule Library

The result is printed successfully.

Schedule.run_pending() Method in Python

The simple schedule.run_pending() method is used to run the pending and scheduled task with the schedule library in Python. To execute this function, we need to import the schedule library. The schedule.run_pending function will execute the pending task simultaneously. Let’s see the code.

import schedule
import time
def job():
    print("Running job")
schedule.every(5).seconds.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)

In this method, we are running the task using the job() function, and after every 5 seconds, this pending task will execute. Let’s see the results to understand the execution.

Schedule Run Pending Method
Schedule Run Pending Method

After every 5 seconds, the statement is printing i.e. the pending task is executed by the program.

Schedule.cancel_job Method

In this method, we can cancel the scheduled task according to specific time interval. We always pass the job as an object argument to this function in Python. Let’s see the code to understand the implementation.

import schedule
import time
def job():
    print("Running job")
my_job = schedule.every(2).seconds.do(job)
time.sleep(1)
schedule.cancel_job(my_job)

In this example, we are importing the schedule library to use the schedule.cancel_job method. We are providing the do() method to pass the task as an argument in Python.

Cancel Schedule Task
Cancel Schedule Task

In this method, we are using a schedule.cancel_job() function to cancel the ongoing and scheduled task.

Importance Of Async Function in Python

Efficient utilization of syste­m resources can greatly e­nhance performance by le­veraging async functions. These functions e­nable the simultaneous e­xecution of multiple tasks without hindering program e­xecution, allowing for improved responsive­ness and preventing fre­ezing. By incorporating non-blocking I/O operations, async functions ensure­ that other tasks can proceed while­ waiting, maintaining overall program responsivene­ss.

Applications have the­ capability to efficiently scale by le­veraging async functions. These functions e­xcel in handling multiple operations simultane­ously. They achieve this through non-blocking input/output and task sche­duling, enabling them to effe­ctively manage heavy workloads without re­quiring excessive re­sources.

The async/await syntax e­nhances the readability and manage­ability of code by following a natural flow of execution. Python librarie­s and frameworks widely support asynchronous programming, providing seamle­ss integration within your code and unlocking the be­nefits of their asynchronous capabilities and e­cosystem.

Applications have the­ capability to efficiently scale by le­veraging async functions. These functions e­xcel in handling multiple operations simultane­ously. They achieve this through non-blocking input/output and task sche­duling, enabling them to effe­ctively manage heavy workloads without re­quiring excessive re­sources.

Summary

In this article, we have seen the async function and how to use it with the schedule library. There are different function in Python which is used to implement the async function in Python. We have implemented the basic example of the async function with the schedule library and also implemented some important functions like schedule.run_pending(), and schedule.cancel_job(). In the end, the importance of the async function in Python is explained in detail. Hope you will enjoy this article.

References

Do read the official documentation on asyncio library.