{"id":112,"date":"2017-04-12T14:21:15","date_gmt":"2017-04-12T14:21:15","guid":{"rendered":"https:\/\/159.69.80.24\/blog\/asynchronous-programming-in-python-asyncio\/"},"modified":"2025-09-11T17:18:17","modified_gmt":"2025-09-11T17:18:17","slug":"asynchronous-programming-in-python-asyncio","status":"publish","type":"post","link":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/","title":{"rendered":"Python\/Django AsyncIO Tutorial with Examples"},"content":{"rendered":"<p>If your developers are diving into async, check our quick tutorial for Python\/Django\u2014 and, when you need line-by-line inspection, <a href=\"https:\/\/djangostars.com\/blog\/debugging-python-applications-with-pdb\/\">Python pdb<\/a> is a simple built-in you can use alongside it.<\/p>\n<p>Note: you can successfully use Python without knowing that asynchronous paradigm even exists. However, if you are interested in how things work under the hood, asyncio is absolutely worth checking.<\/p>\n<p>By the way, we regularly use AsyncIO with Python in our projects, and this really helps to make <a href=\"https:\/\/djangostars.com\/case-studies\/\">successful products<\/a>.<\/p>\n<h2>What Asynchronous is All About?<\/h2>\n<p>To start with, in a classical sequential programming, all the instructions you send to the interpreter will be executed one by one. It is easy to visualize and predict the output of such a code. But\u2026<\/p>\n<p>Say you have a script that requests data from 3 different servers. Sometimes, depending on who knows what, the request to one of those servers may take unexpectedly too much time to execute. For instance, imagine that it takes 10 seconds to get data from the second server. While you are waiting, the whole script is actually doing nothing. However, what if you could write a script that instead of waiting for the second request\u00a0could simply skip it and start executing the third request, then go back to the second one, and proceed from where it left. That&#8217;s it. You minimize idle time by switching tasks.<\/p>\n<p>Still, you don\u2019t want to use an asynchronous code when you need a simple script, with little to no I\/O.<br \/>\nIn addition, one more important thing to mention is that all the code is running in a single thread. Thus, if you expect that one part of the program will be executed in the background while your program will be doing something else, this won\u2019t happen.<\/p>\n<p>All of this applies to the asynchronous Python basics, but we dare to assume that you know this without us. So let&#8217;s jump straight into examples and the Python AsyncIO tutorial.<\/p>\n<h2>Getting Started the AsyncIO Tutorial<\/h2>\n<p>Here are the most basic definitions of asyncio main concepts:<\/p>\n<ul>\n<li><strong>Coroutine<\/strong> \u2014 generator that consumes data, but doesn\u2019t generate it. Python 2.5 introduced a new syntax that made it possible to send a value to a generator. I recommend checking David Beazley <a href=\"http:\/\/www.dabeaz.com\/coroutines\/Coroutines.pdf\" target=\"_blank\" rel=\"nofollow noopener\">\u201cA Curious Course on Coroutines and Concurrency\u201d<\/a> for a detailed description of coroutines.<\/li>\n<li><strong>Tasks<\/strong> \u2014 schedulers for coroutines. If you check a source code below, you\u2019ll see that it just says <code>event_loop<\/code> to run its <code>_step<\/code> as soon as possible, meanwhile <code>_step<\/code> just calls next step of coroutine.<\/li>\n<\/ul>\n<pre><code class=\"language-language python\">class Task(futures.Future):\r\n    def __init__(self, coro, loop=None):\r\n        super().__init__(loop=loop)\r\n        ...\r\n        self._loop.call_soon(self._step)\r\n    def _step(self):\r\n            ...\r\n        try:\r\n            ...\r\n            result = next(self._coro)\r\n        except StopIteration as exc:\r\n            self.set_result(exc.value)\r\n        except BaseException as exc:\r\n            self.set_exception(exc)\r\n            raise\r\n        else:\r\n            ...\r\n            self._loop.call_soon(self._step)\r\n<\/code><\/pre>\n<ul>\n<li><strong>Event Loop<\/strong> \u2014 think of it as the central executor in asyncio.<\/li>\n<\/ul>\n<p>Now let&#8217;s check how all these work together. As I&#8217;ve already mentioned, asynchronous code is running in a single thread:<br \/>\n<img decoding=\"async\" class=\"alignnone size-full wp-image-586\" src=\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2023\/01\/AsyncIO-Event-Loop.png\" alt=\"AsyncIO-Event-Loop\" width=\"1989\" height=\"2901\" \/><!--<img decoding=\"async\" class=\"alignnone size-full wp-image-586\" src=\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_1-2.png\" alt=\"\" width=\"1989\" height=\"2901\" \/>--><br \/>\nAs you can see from the chart:<\/p>\n<ul>\n<li>The event loop is running in a thread<\/li>\n<li>It gets tasks from the queue<\/li>\n<li>Each task calls next step of a coroutine<\/li>\n<li>If coroutine calls another coroutine (<code>await &lt;coroutine_name&gt;<\/code> ), current coroutine gets suspended and context switch occurs. Context of the current coroutine(variables, state) is saved and context of a called coroutine is loaded<\/li>\n<li>If coroutine comes across a blocking code(I\/O, sleep), the current coroutine gets suspended and control is passed back to the event loop<\/li>\n<li>Event loop gets next tasks from the queue 2, &#8230;n<\/li>\n<li>Then the event loop goes back to task 1 from where it left<\/li>\n<\/ul>\n<h2>Asynchronous vs Synchronous Code<\/h2>\n<p>Let\u2019s try to prove that asynchronous approach really works. I will compare two scripts, that are nearly identical, except the <code>sleep<\/code> method. In the first one I am going to use a standard <code>time.sleep<\/code>, and in the second one \u2014 <code>asyncio.sleep<\/code><br \/>\nSleep is used here because it is the simplest way to show the main idea, how asyncio handles I\/O.<br \/>\nHere we use synchronous sleep inside async code:<\/p>\n<pre><code class=\"language-language python\">import asyncio\r\nimport time\r\nfrom datetime import datetime\r\nasync def custom_sleep():\r\n    print('SLEEP', datetime.now())\r\n    time.sleep(1)\r\nasync def factorial(name, number):\r\n    f = 1\r\n    for i in range(2, number+1):\r\n        print('Task {}: Compute factorial({})'.format(name, i))\r\n        await custom_sleep()\r\n        f *= i\r\n    print('Task {}: factorial({}) is {}n'.format(name, number, f))\r\nstart = time.time()\r\nloop = asyncio.get_event_loop()\r\ntasks = [\r\n    asyncio.ensure_future(factorial(\"A\", 3)),\r\n    asyncio.ensure_future(factorial(\"B\", 4)),\r\n]\r\nloop.run_until_complete(asyncio.wait(tasks))\r\nloop.close()\r\nend = time.time()\r\nprint(\"Total time: {}\".format(end - start))\r\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code class=\"language-language python \">Task A: Compute factorial(2)\r\nSLEEP 2017-04-06 13:39:56.207479\r\nTask A: Compute factorial(3)\r\nSLEEP 2017-04-06 13:39:57.210128\r\nTask A: factorial(3) is 6\r\nTask B: Compute factorial(2)\r\nSLEEP 2017-04-06 13:39:58.210778\r\nTask B: Compute factorial(3)\r\nSLEEP 2017-04-06 13:39:59.212510\r\nTask B: Compute factorial(4)\r\nSLEEP 2017-04-06 13:40:00.217308\r\nTask B: factorial(4) is 24\r\nTotal time: 5.016386032104492\r\n<\/code><\/pre>\n<p>Now the same code, but with the asynchronous sleep method:<\/p>\n<pre><code class=\"language-language python\">import asyncio\r\nimport time\r\nfrom datetime import datetime\r\nasync def custom_sleep():\r\n    print('SLEEP {}n'.format(datetime.now()))\r\n    await asyncio.sleep(1)\r\nasync def factorial(name, number):\r\n    f = 1\r\n    for i in range(2, number+1):\r\n        print('Task {}: Compute factorial({})'.format(name, i))\r\n        await custom_sleep()\r\n        f *= i\r\n    print('Task {}: factorial({}) is {}n'.format(name, number, f))\r\nstart = time.time()\r\nloop = asyncio.get_event_loop()\r\ntasks = [\r\n    asyncio.ensure_future(factorial(\"A\", 3)),\r\n    asyncio.ensure_future(factorial(\"B\", 4)),\r\n]\r\nloop.run_until_complete(asyncio.wait(tasks))\r\nloop.close()\r\nend = time.time()\r\nprint(\"Total time: {}\".format(end - start))\r\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code class=\"language-language python\">Task A: Compute factorial(2)\r\nSLEEP 2017-04-06 13:44:40.648665\r\nTask B: Compute factorial(2)\r\nSLEEP 2017-04-06 13:44:40.648859\r\nTask A: Compute factorial(3)\r\nSLEEP 2017-04-06 13:44:41.649564\r\nTask B: Compute factorial(3)\r\nSLEEP 2017-04-06 13:44:41.649943\r\nTask A: factorial(3) is 6\r\nTask B: Compute factorial(4)\r\nSLEEP 2017-04-06 13:44:42.651755\r\nTask B: factorial(4) is 24\r\nTotal time: 3.008226156234741\r\n<\/code><\/pre>\n<p>As you can see, the asynchronous version is 2 seconds faster. When async sleep is used (each time we call <code>await asyncio.sleep(1)<\/code>), control is passed back to the event loop, that runs another task from the queue(either Task A or Task B).<br \/>\nIn a case of standard sleep &#8211; nothing happens, a thread just hangs out. In fact, because of a standard sleep, current thread releases a python interpreter, and it can work with other threads if they exist, but it is another topic.<br \/>\n<div class=\"new_shortcode_box shortcode_service_box services\">\n\t\t<div class=\"content_holder\">\n\t\t    <div class=\"shortcode_logo\">\n\t\t        <svg width=\"110\" height=\"18\" viewBox=\"0 0 172 28\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                                <path class=\"black_fill\" fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M118.097 20.0083C116.709 20.0083 115.244 19.6247 114.765 19.6247C114.234 19.6247 113.73 20.0083 113.73 21.386C113.73 21.386 115.548 22.203 118.274 22.203C122.894 22.203 124.056 20.3143 124.056 17.9403C124.056 15.0816 122.591 14.3924 119.941 13.4733C117.466 12.6058 116.407 12.3245 116.407 10.9464C116.407 9.74677 117.213 9.21048 118.401 9.21048C120.142 9.21048 122.011 9.79764 122.314 9.79764C122.768 9.79764 123.323 9.18534 123.323 8.18957C123.323 8.18957 121.405 7.09245 118.855 7.09245C114.815 7.09245 113.78 9.18535 113.78 11.0229C113.78 13.3966 114.74 14.2903 116.355 14.9284C119.208 16.0514 121.178 16.2814 121.178 17.9657C121.178 19.2683 120.597 20.0083 118.097 20.0083ZM41.5998 7.62864C40.7161 7.32214 39.6555 7.09294 38.2919 7.09294C33.8237 7.09294 31.6021 9.87453 31.6021 14.9035C31.6021 19.3962 33.1925 22.2034 37.3329 22.2034C39.4786 22.2034 40.8927 21.0034 41.5745 20.1872L41.5998 20.8768C41.6504 21.3358 41.827 21.923 42.7606 21.923H44.301V2.75296H42.7606C41.8017 2.75296 41.5998 3.31428 41.5998 3.82504V7.62864ZM41.5998 17.3273C41.5998 17.3273 39.984 20.0339 37.4843 20.0339C35.0104 20.0339 34.3792 18.1195 34.3792 14.6731C34.3792 11.4567 35.061 9.23631 38.6458 9.23631C39.6052 9.23631 40.6149 9.51678 41.5998 9.9254V17.3273ZM78.0813 21.9225H80.7576V11.814C80.7576 8.1384 78.8387 7.09245 74.9759 7.09245C71.3408 7.09245 68.4371 9.00652 68.4371 9.00652V21.9225H71.1133V10.0783C71.1133 10.0783 72.9813 9.23631 74.3955 9.23631C77.3998 9.23631 78.0813 9.97626 78.0813 11.5334V21.9225ZM134.357 21.4632C134.357 21.0034 134.306 20.4676 133.978 19.7789C133.978 19.7789 132.816 20.0087 131.352 20.0087C129.887 20.0087 129.483 19.5744 129.483 18.4003V9.44057H131.984C132.539 9.44057 133.12 9.28728 133.12 8.36849V7.34767H129.433V2.85499H127.944C126.984 2.85499 126.807 3.36545 126.807 3.8764V18.7063C126.807 21.106 128.171 22.2034 130.998 22.2034C133.17 22.2034 134.23 21.6165 134.23 21.6165L134.357 21.4632ZM151.903 7.50048V21.9228H154.579V11.4062L159.578 9.56834C160.082 9.38981 160.31 9.10914 160.31 8.64906C160.31 8.24073 160.134 7.62844 159.805 6.91332C159.805 6.91332 156.397 8.08773 154.579 9.15971V8.47102C154.579 7.96007 154.402 7.50048 153.443 7.50048H151.903ZM145.718 17.3273C145.718 17.3273 144.127 20.0339 141.577 20.0339C139.178 20.0339 138.548 18.1195 138.548 14.6731C138.548 11.4567 139.229 9.23631 142.813 9.23631C143.773 9.23631 144.758 9.51678 145.718 9.9254V17.3273ZM111.483 14.673C111.483 9.87414 109.437 7.09245 104.792 7.09245C100.223 7.09245 98.1269 9.87414 98.1269 14.673C98.1269 19.3957 100.223 22.203 104.792 22.203C109.387 22.203 111.483 19.3957 111.483 14.673ZM108.705 14.6731C108.705 18.3485 107.621 20.0843 104.792 20.0843C102.041 20.0843 100.904 18.3485 100.904 14.6731C100.904 10.9465 102.066 9.21048 104.792 9.21048C107.519 9.21048 108.705 10.9465 108.705 14.6731ZM97.0412 7.32165H92.0425C91.3864 7.16894 90.1741 7.09245 89.518 7.09245C85.9839 7.09245 83.8879 8.77672 83.8879 11.687C83.8879 12.8861 84.166 14.1629 85.3525 15.1838C84.3421 15.8221 84.166 16.5874 84.166 17.4811C84.166 18.2981 84.3679 19.0889 85.2506 19.6764C84.2926 20.1613 83.207 21.438 83.207 23.3521C83.207 26.3896 85.403 27.9465 89.5684 27.9465C93.81 27.9465 95.9308 26.3896 95.9308 23.3521C95.9308 20.3654 93.7091 18.8082 89.3665 18.8082C87.0181 18.8082 86.5639 18.3999 86.5639 17.4811C86.5639 16.5112 87.2962 16.2302 89.518 16.2302C94.3395 16.2302 95.1728 13.7034 95.1728 11.687C95.1728 10.8447 94.9461 10.0783 94.5671 9.38961H95.9061C96.486 9.38961 97.0412 9.23631 97.0412 8.31753V7.32165ZM89.5684 20.8766C92.2949 20.8766 93.2549 21.3356 93.2549 23.3523C93.2549 25.4199 92.2197 25.8788 89.5684 25.8788C87.0181 25.8788 85.883 25.4199 85.883 23.3523C85.883 21.3356 86.7925 20.8766 89.5684 20.8766ZM92.472 11.6872C92.472 13.2949 91.9921 14.2904 89.4675 14.2904C86.9676 14.2649 86.5639 13.2949 86.5639 11.6872C86.5639 10.1297 87.0181 9.15971 89.518 9.15971C92.0425 9.15971 92.472 10.1297 92.472 11.6872ZM65.3818 21.9225V9.00652C65.3818 9.00652 63.0592 7.09245 59.3737 7.09245C54.9296 7.09245 52.7086 9.87414 52.7086 14.9031C52.7086 19.3957 54.2987 22.203 58.4394 22.203C60.56 22.203 61.9739 21.003 62.6554 20.1868L62.7062 20.8764C62.7311 21.3354 62.9333 21.9225 63.8423 21.9225H65.3818ZM62.7062 17.3273C62.7062 17.3273 61.0651 20.0339 58.5651 20.0339C56.1671 20.0339 55.485 18.1195 55.485 14.6731C55.485 11.4567 56.1671 9.23631 59.752 9.23631C60.7619 9.23631 61.696 9.51678 62.7062 9.9254V17.3273ZM47.4823 5.94357H48.6182C49.8046 5.94357 50.2593 5.25458 50.2593 4.23287C50.2593 3.23778 49.8805 2.625 48.6683 2.625C47.7095 2.625 47.4823 3.18662 47.4823 3.69708V5.94357ZM47.4316 21.1566C47.4316 23.5819 44.5535 26.5174 44.5535 26.5174C44.7051 27.1813 45.5132 27.7686 46.144 27.9983C46.144 27.9983 50.1077 24.7313 50.1077 21.897V8.95565C50.1077 8.47112 49.8805 7.96007 48.9216 7.96007H47.4316V21.1566ZM148.394 21.9225V9.00652C148.394 9.00652 146.121 7.09245 142.435 7.09245C137.941 7.09245 135.77 9.87414 135.77 14.9031C135.77 19.3957 137.36 22.203 141.501 22.203C143.647 22.203 145.036 20.9526 145.718 20.11V20.8764C145.718 21.386 145.944 21.9225 146.904 21.9225H148.394ZM166.04 20.0083C164.653 20.0083 163.188 19.6247 162.708 19.6247C162.179 19.6247 161.673 20.0083 161.673 21.386C161.673 21.386 163.491 22.203 166.217 22.203C170.838 22.203 172 20.3143 172 17.9403C172 15.0816 170.535 14.3924 167.884 13.4733C165.41 12.6058 164.35 12.3245 164.35 10.9464C164.35 9.74677 165.157 9.21048 166.344 9.21048C168.086 9.21048 169.954 9.79764 170.257 9.79764C170.711 9.79764 171.267 9.18534 171.267 8.18957C171.267 8.18957 169.349 7.09245 166.798 7.09245C162.758 7.09245 161.724 9.18535 161.724 11.0229C161.724 13.3966 162.683 14.2903 164.299 14.9284C167.152 16.0514 169.121 16.2814 169.121 17.9657C169.121 19.2683 168.54 20.0083 166.04 20.0083Z\" fill=\"#ffffff\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M11.2642 0.259286C11.3489 0.12994 11.3062 0 11.1362 0H2.9011C1.83466 0 1.62109 0.345285 1.62109 1.20825L8.74662 4.3569C8.95979 4.48635 9.17276 4.40065 9.25837 4.22756L11.2642 0.259286Z\" fill=\"#005BBB\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M23.2525 24.2454C24.0642 24.2454 26.3247 22.3467 26.3247 15.6174C26.3247 15.3582 26.1542 15.2727 25.8559 15.3582L21.2901 17.0405C21.0766 17.1275 21.1198 17.257 21.1624 17.4289L23.2525 24.2454Z\" fill=\"#FFC700\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M21.3324 2.28598C21.3324 1.3373 18.0902 0 13.2264 0C13.0558 0 13.0129 0.12994 13.0984 0.259286L15.2319 4.2713C15.3169 4.4435 15.4022 4.48635 15.6161 4.40065L21.3324 2.28598Z\" fill=\"#005BBB\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M4.82115 10.3963C4.99226 10.3097 5.03432 10.2238 4.94881 10.0943L1.15204 3.62305C0.340933 3.62305 0 3.75299 0 4.9175V12.8124C0 12.9415 0.0847133 13.0709 0.255922 12.9844L4.82115 10.3963Z\" fill=\"#005BBB\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M0.255922 14.8835C0.0847133 14.797 0 14.9264 0 15.0554V22.95C0 24.1152 0.340933 24.2446 1.15204 24.2446L4.94881 17.7732C5.03432 17.6444 4.99226 17.5574 4.82115 17.4717L0.255922 14.8835Z\" fill=\"#005BBB\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9.25837 23.7698C9.17276 23.5979 8.95979 23.5108 8.74662 23.6404L1.62109 26.7902C1.62109 27.6521 1.83466 27.9977 2.9011 27.9977H11.1362C11.3062 27.9977 11.3489 27.8689 11.2642 27.7386L9.25837 23.7698Z\" fill=\"#FFC700\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M13.2264 27.9979C18.0902 27.9979 21.3324 26.6612 21.3324 25.7115L15.5728 23.5982C15.3598 23.5111 15.2743 23.555 15.1887 23.7278L13.0984 27.7389C13.0129 27.8692 13.0558 27.9979 13.2264 27.9979Z\" fill=\"#FFC700\"><\/path>\n                                <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M21.1196 10.4391C21.0764 10.6125 21.034 10.7419 21.2478 10.8276L25.9404 12.5538C26.154 12.6397 26.2816 12.5099 26.2816 12.2511C26.538 8.58466 25.2154 3.23564 23.2099 3.62348L21.1196 10.4391Z\" fill=\"#005BBB\"><\/path>\n                            <\/svg>\n            <\/div>\n\t\t\t<div class=\"title\">Python &amp; Django development.<\/div>\n\t\t\t<div class=\"description\">Your chance to enter the market faster.<\/div>\n\t\t\t<div class=\"link\">\n\t\t\t<a href=\"https:\/\/djangostars.com\/services\/python-django-development\/\"class=\"link\">\n                        <span>Learn more<\/span>\n                        <div class=\"button_animated\">\n                            <svg width=\"24\" height=\"12\" viewBox=\"0 0 24 12\" fill=\"none\"\n                                 xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                                <path d=\"M23.725 5.33638C23.7248 5.3361 23.7245 5.33577 23.7242 5.33549L18.8256 0.460497C18.4586 0.0952939 17.865 0.096653 17.4997 0.463684C17.1345 0.830668 17.1359 1.42425 17.5028 1.7895L20.7918 5.06249H0.9375C0.419719 5.06249 0 5.48221 0 5.99999C0 6.51777 0.419719 6.93749 0.9375 6.93749H20.7917L17.5029 10.2105C17.1359 10.5757 17.1345 11.1693 17.4998 11.5363C17.865 11.9034 18.4587 11.9046 18.8256 11.5395L23.7242 6.66449C23.7245 6.66421 23.7248 6.66388 23.7251 6.6636C24.0923 6.29713 24.0911 5.70163 23.725 5.33638Z\"\n                                      fill=\"#282828\"><\/path>\n                            <\/svg>\n                            <div class=\"shape\"><\/div>\n                        <\/div>\n                    <\/a>\n\t\t<\/div>\n\t\t<\/div>\n\t\t<div class=\"illustration\">\n\t\t\t<img decoding=\"async\" src=\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2022\/07\/Services-Python-Django-2.png\" alt=\"shortcode-image\">\n\t\t<\/div>\n\t<\/div><\/p>\n<h2>Several Reasons to Stick to Asynchronous Programming<\/h2>\n<p>First of all, <a href=\"https:\/\/djangostars.com\/blog\/top-seven-apps-built-python\/\">applications made with Python<\/a> like Facebook use asynchronous a lot. Facebook\u2019s React Native and RocksDB think asynchronous. How do think it is possible for, let&#8217;s say, Twitter to handle more than five billion sessions a day?<\/p>\n<p>So, why not refactor the code or change the approach so that software could work faster?<\/p>\n<p><b>If you are considering using asynchronous programming in your <a href=\"https:\/\/djangostars.com\/case-studies\/\">project<\/a> and are looking for a <a href=\"https:\/\/djangostars.com\/services\/python-development\/\">Python development agency<\/a>, <a href=\"https:\/\/djangostars.com\/get-in-touch\/\">contact<\/a> the Django Stars team<\/b>.<\/p>\n<div class=\"dj-main-article-faq\" style=\"padding-top: 0px;\">\n\t\t<div class=\"dj-main-article-faq-title\">\n\t\tFrequently Asked Questions\n\t\t<\/div>\n\t\t<div class=\"dj-main-article-faq-items\">\n\t\t\t<div class=\"dj-main-article-faq-accordeon accordeon\"><dl>\n\t\t\t\t<dt>What is async within Python? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>When using asynchronous programming in Python, a task can be executed separately from the main application execution thread. When the task completes, the execution of those that follow it continues. The benefits of async include improved application performance and improved response speed. One of Python's tools for writing asynchronous code is the AsyncIO module.<\/dd>\n\t\t\t<\/dl><dl>\n\t\t\t\t<dt>Why do we need AsyncIO for using Python? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>AsyncIO is a library that helps to write async Python code and minimize idle time by switching tasks. This is a way to improve the speed of Python code execution.<\/dd>\n\t\t\t<\/dl><dl>\n\t\t\t\t<dt>What are the real use cases of async in Python? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>For example, you have a script that requests data from 3 different servers. Sometimes the request to one of those servers may take too much time to execute. While waiting, the whole script is doing nothing. However, instead of waiting for the second request, an asynchronous script could simply skip it and start executing the third request, then go back to the second one and proceed from where it left.<\/dd>\n\t\t\t<\/dl><dl>\n\t\t\t\t<dt>How will using async programming in Python help my project? \n\t\t\t\t<div class=\"cross\">\n\t\t\t\t<span><\/span>\n\t\t\t\t<span><\/span>\n\t\t\t\t<\/div>\n\t\t\t\t<\/dt>\n\t\t\t\t<dd>You can refactor the code or change the approach using asynchronous programming in your project so that the software work faster. Productivity is improved by reducing idle time when moving from task to task.<\/dd>\n\t\t\t<\/dl><\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If your developers are diving into async, check our quick tutorial for Python\/Django\u2014 and, when you need line-by-line inspection, Python pdb is a simple built-in you can use alongside it. Note: you can successfully use Python without knowing that asynchronous paradigm even exists. However, if you are interested in how things work under the hood, [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":3543,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[44],"tags":[30,7],"class_list":["post-112","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-django","tag-backend","tag-case-studies"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Software Development Blog &amp; IT Tech Insights | Django Stars<\/title>\n<meta name=\"description\" content=\"If you have decided to learn the asynchronous part of Python, here is an \u201cAsyncio\u201d. You will observe some examples and notice our point of view about it.\" \/>\n<link rel=\"canonical\" href=\"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/112\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\/Django AsyncIO Tutorial: Async Programming in Python\" \/>\n<meta property=\"og:description\" content=\"If you have decided to learn the asynchronous part of Python, here is an \u201cAsyncio\u201d. You will observe some examples and notice our point of view about it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/\" \/>\n<meta property=\"og:site_name\" content=\"Software Development Blog &amp; IT Tech Insights | Django Stars\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/djangostars\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/a.ryabtsev\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-12T14:21:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-11T17:18:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Alexander Ryabtsev\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@djangostars\" \/>\n<meta name=\"twitter:site\" content=\"@djangostars\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alexander Ryabtsev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/\"},\"author\":{\"name\":\"Alexander Ryabtsev\",\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f1a566bbee334235e6f57edd6930fdc1\"},\"headline\":\"Python\/Django AsyncIO Tutorial with Examples\",\"datePublished\":\"2017-04-12T14:21:15+00:00\",\"dateModified\":\"2025-09-11T17:18:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/\"},\"wordCount\":846,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg\",\"keywords\":[\"Backend\",\"Case Study\"],\"articleSection\":[\"Python &amp; Django\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/\",\"url\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/\",\"name\":\"Python\/Django AsyncIO Tutorial: Async Programming in Python\",\"isPartOf\":{\"@id\":\"https:\/\/djangostars.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg\",\"datePublished\":\"2017-04-12T14:21:15+00:00\",\"dateModified\":\"2025-09-11T17:18:17+00:00\",\"author\":{\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f1a566bbee334235e6f57edd6930fdc1\"},\"description\":\"If you have decided to learn the asynchronous part of Python, here is an \u201cAsyncio\u201d. You will observe some examples and notice our point of view about it.\",\"breadcrumb\":{\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage\",\"url\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg\",\"contentUrl\":\"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg\",\"width\":1440,\"height\":620,\"caption\":\"Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/djangostars.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\/Django AsyncIO Tutorial with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/djangostars.com\/blog\/#website\",\"url\":\"https:\/\/djangostars.com\/blog\/\",\"name\":\"Software Development Blog &amp; IT Tech Insights | Django Stars\",\"description\":\"Welcome behind the scenes of software product development. We share our best practices, tech solutions, management tips, and every useful insight we\u2018ve got while working on our projects.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/djangostars.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f1a566bbee334235e6f57edd6930fdc1\",\"name\":\"Alexander Ryabtsev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/djangostars.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c456852d26226ecd8bc156a7339fc1f425a6774e8f9e07a977c060e2ecedebb9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c456852d26226ecd8bc156a7339fc1f425a6774e8f9e07a977c060e2ecedebb9?s=96&d=mm&r=g\",\"caption\":\"Alexander Ryabtsev\"},\"sameAs\":[\"https:\/\/www.facebook.com\/a.ryabtsev\",\"https:\/\/www.linkedin.com\/in\/alexander-ryabtsev\/\"],\"url\":\"https:\/\/djangostars.com\/blog\/author\/alexander-ryabtsev\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Software Development Blog &amp; IT Tech Insights | Django Stars","description":"If you have decided to learn the asynchronous part of Python, here is an \u201cAsyncio\u201d. You will observe some examples and notice our point of view about it.","canonical":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/112","og_locale":"en_US","og_type":"article","og_title":"Python\/Django AsyncIO Tutorial: Async Programming in Python","og_description":"If you have decided to learn the asynchronous part of Python, here is an \u201cAsyncio\u201d. You will observe some examples and notice our point of view about it.","og_url":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/","og_site_name":"Software Development Blog &amp; IT Tech Insights | Django Stars","article_publisher":"https:\/\/www.facebook.com\/djangostars\/","article_author":"https:\/\/www.facebook.com\/a.ryabtsev","article_published_time":"2017-04-12T14:21:15+00:00","article_modified_time":"2025-09-11T17:18:17+00:00","og_image":[{"width":1440,"height":620,"url":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg","type":"image\/jpeg"}],"author":"Alexander Ryabtsev","twitter_card":"summary_large_image","twitter_creator":"@djangostars","twitter_site":"@djangostars","twitter_misc":{"Written by":"Alexander Ryabtsev","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#article","isPartOf":{"@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/"},"author":{"name":"Alexander Ryabtsev","@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f1a566bbee334235e6f57edd6930fdc1"},"headline":"Python\/Django AsyncIO Tutorial with Examples","datePublished":"2017-04-12T14:21:15+00:00","dateModified":"2025-09-11T17:18:17+00:00","mainEntityOfPage":{"@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/"},"wordCount":846,"commentCount":0,"image":{"@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage"},"thumbnailUrl":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg","keywords":["Backend","Case Study"],"articleSection":["Python &amp; Django"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/","url":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/","name":"Python\/Django AsyncIO Tutorial: Async Programming in Python","isPartOf":{"@id":"https:\/\/djangostars.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage"},"image":{"@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage"},"thumbnailUrl":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg","datePublished":"2017-04-12T14:21:15+00:00","dateModified":"2025-09-11T17:18:17+00:00","author":{"@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f1a566bbee334235e6f57edd6930fdc1"},"description":"If you have decided to learn the asynchronous part of Python, here is an \u201cAsyncio\u201d. You will observe some examples and notice our point of view about it.","breadcrumb":{"@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#primaryimage","url":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg","contentUrl":"https:\/\/djangostars.com\/blog\/wp-content\/uploads\/2021\/12\/Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python.jpg","width":1440,"height":620,"caption":"Python-Asyncio-Tutorial.-Asynchronous-Programming-in-Python"},{"@type":"BreadcrumbList","@id":"https:\/\/djangostars.com\/blog\/asynchronous-programming-in-python-asyncio\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/djangostars.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python\/Django AsyncIO Tutorial with Examples"}]},{"@type":"WebSite","@id":"https:\/\/djangostars.com\/blog\/#website","url":"https:\/\/djangostars.com\/blog\/","name":"Software Development Blog &amp; IT Tech Insights | Django Stars","description":"Welcome behind the scenes of software product development. We share our best practices, tech solutions, management tips, and every useful insight we\u2018ve got while working on our projects.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/djangostars.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/f1a566bbee334235e6f57edd6930fdc1","name":"Alexander Ryabtsev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/djangostars.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c456852d26226ecd8bc156a7339fc1f425a6774e8f9e07a977c060e2ecedebb9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c456852d26226ecd8bc156a7339fc1f425a6774e8f9e07a977c060e2ecedebb9?s=96&d=mm&r=g","caption":"Alexander Ryabtsev"},"sameAs":["https:\/\/www.facebook.com\/a.ryabtsev","https:\/\/www.linkedin.com\/in\/alexander-ryabtsev\/"],"url":"https:\/\/djangostars.com\/blog\/author\/alexander-ryabtsev\/"}]}},"_links":{"self":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":21,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":9675,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions\/9675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/media\/3543"}],"wp:attachment":[{"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/djangostars.com\/blog\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}