{"id":52873,"date":"2023-06-30T09:28:45","date_gmt":"2023-06-30T09:28:45","guid":{"rendered":"https:\/\/www.askpython.com\/?p=52873"},"modified":"2023-06-30T09:28:46","modified_gmt":"2023-06-30T09:28:46","slug":"async-function-schedule-library","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/async-function-schedule-library","title":{"rendered":"Async Function Using Schedule Library"},"content":{"rendered":"\n<p>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 <a href=\"https:\/\/www.askpython.com\/python-modules\/sched-module\" data-type=\"post\" data-id=\"11632\">schedule library<\/a>. In this article, we are going to see how these async functions will be executed in Python. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schedule Library in Python<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>The combination of asyncio and the schedule library always helps to execute the asynchronous tasks along with the scheduled tasks. Let&#8217;s see the simple code to combine both libraries in one frame. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schedule Library With Async Function<\/h2>\n\n\n\n<p>To use the schedule library with the async function, we need to install the asyncio and schedule libraries first. Let&#8217;s see how to install these libraries in Python.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\npip install schedule\npip install asyncio\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"325\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/installation-of-libraries-for-async-function-1024x325.png\" alt=\"Installation Of Libraries For Async Function\" class=\"wp-image-53130\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/installation-of-libraries-for-async-function-1024x325.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/installation-of-libraries-for-async-function-300x95.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/installation-of-libraries-for-async-function-768x244.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/installation-of-libraries-for-async-function-1536x488.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/installation-of-libraries-for-async-function.png 1603w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Installation Of Libraries For Async Function<\/figcaption><\/figure>\n\n\n\n<p>Now let&#8217;s execute the code using both the libraries, where scheduled tasks and asynchronous tasks are arranged in a non-blocking way.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport schedule\nimport time\nimport threading\nimport asyncio\n\nasync def async_task():\n    print(&quot;Async task is running&quot;)\ndef run_scheduler():\n    while True:\n        schedule.run_pending()\n        time.sleep(1)\nasync def main():\n    schedule.every(5).seconds.do(async_task)\n    scheduler_thread = threading.Thread(target=run_scheduler)\n    scheduler_thread.start()   \n    while True:\n        await asyncio.sleep(1)\n   \n<\/pre><\/div>\n\n\n<p>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. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"155\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/async-function-with-schedule-library-1024x155.png\" alt=\"Async Function With Schedule Library\" class=\"wp-image-53229\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/async-function-with-schedule-library-1024x155.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/async-function-with-schedule-library-300x46.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/async-function-with-schedule-library-768x117.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/async-function-with-schedule-library.png 1436w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Async Function With Schedule Library<\/figcaption><\/figure>\n\n\n\n<p>The result is printed successfully. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schedule.run_pending() Method in Python<\/h2>\n\n\n\n<p>The simple schedule.run_pending() method is used to run the pending and <a href=\"https:\/\/www.askpython.com\/python\/examples\/execute-python-windows-task-scheduler\" data-type=\"post\" data-id=\"26525\">scheduled task<\/a> 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&#8217;s see the code. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport schedule\nimport time\ndef job():\n    print(&quot;Running job&quot;)\nschedule.every(5).seconds.do(job)\nwhile True:\n    schedule.run_pending()\n    time.sleep(1)\n<\/pre><\/div>\n\n\n<p>In this method, we are running the task using the job() function, and after every 5 seconds, this pending task will execute. Let&#8217;s see the results to understand the execution.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"543\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/schedule.run_pending-method-1024x543.png\" alt=\"Schedule Run Pending Method\" class=\"wp-image-53240\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/schedule.run_pending-method-1024x543.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/schedule.run_pending-method-300x159.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/schedule.run_pending-method-768x407.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/schedule.run_pending-method-1536x814.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/schedule.run_pending-method.png 1673w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Schedule Run Pending Method<\/figcaption><\/figure>\n\n\n\n<p>After every 5 seconds, the statement is printing i.e. the pending task is executed by the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schedule.cancel_job Method<\/h2>\n\n\n\n<p>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&#8217;s see the code to understand the implementation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport schedule\nimport time\ndef job():\n    print(&quot;Running job&quot;)\nmy_job = schedule.every(2).seconds.do(job)\ntime.sleep(1)\nschedule.cancel_job(my_job)\n<\/pre><\/div>\n\n\n<p>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. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"118\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/cancel-schedule-task-1024x118.png\" alt=\"Cancel Schedule Task\" class=\"wp-image-53246\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/cancel-schedule-task-1024x118.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/cancel-schedule-task-300x35.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/cancel-schedule-task-768x89.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/06\/cancel-schedule-task.png 1375w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Cancel Schedule Task<\/figcaption><\/figure>\n\n\n\n<p>In this method, we are using a schedule.cancel_job() function to cancel the ongoing and scheduled task. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Importance Of Async Function in Python<\/h2>\n\n\n\n<p>Efficient utilization of syste\u00adm resources can greatly e\u00adnhance performance by le\u00adveraging async functions. These functions e\u00adnable the simultaneous e\u00adxecution of multiple tasks without hindering program e\u00adxecution, allowing for improved responsive\u00adness and preventing fre\u00adezing. By incorporating non-blocking I\/O operations, async functions ensure\u00ad that other tasks can proceed while\u00ad waiting, maintaining overall program responsivene\u00adss. <\/p>\n\n\n\n<p>Applications have the\u00ad capability to efficiently scale by le\u00adveraging async functions. These functions e\u00adxcel in handling multiple operations simultane\u00adously. They achieve this through non-blocking input\/output and task sche\u00adduling, enabling them to effe\u00adctively manage heavy workloads without re\u00adquiring excessive re\u00adsources.<\/p>\n\n\n\n<p>The async\/await syntax e\u00adnhances the readability and manage\u00adability of code by following a natural flow of execution. Python librarie\u00ads and frameworks widely support asynchronous programming, providing seamle\u00adss integration within your code and unlocking the be\u00adnefits of their asynchronous capabilities and e\u00adcosystem.<\/p>\n\n\n\n<p>Applications have the\u00ad capability to efficiently scale by le\u00adveraging async functions. These functions e\u00adxcel in handling multiple operations simultane\u00adously. They achieve this through non-blocking input\/output and task sche\u00adduling, enabling them to effe\u00adctively manage heavy workloads without re\u00adquiring excessive re\u00adsources.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>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. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References <\/h2>\n\n\n\n<p>Do read the official documentation on <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" target=\"_blank\" rel=\"noopener\">asyncio library<\/a>.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":53252,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-52873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/52873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=52873"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/52873\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/53252"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=52873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=52873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=52873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}