{"id":4124,"date":"2022-07-18T01:02:46","date_gmt":"2022-07-18T01:02:46","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4124"},"modified":"2025-04-02T06:56:34","modified_gmt":"2025-04-02T06:56:34","slug":"python-asyncio-create_task","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-create_task\/","title":{"rendered":"Python asyncio.create_task()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use <code>asyncio.create_task()<\/code> function to run multiple tasks concurrently.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='simulating-a-long-running-operation'>Simulating a long-running operation <a href=\"#simulating-a-long-running-operation\" class=\"anchor\" id=\"simulating-a-long-running-operation\" title=\"Anchor for Simulating a long-running operation\">#<\/a><\/h2>\n\n\n\n<p>To simulate a long-running operation, you can use the <code><code>sleep()<\/code><\/code> coroutine of the <code>asyncio<\/code> package. The <code><code>sleep()<\/code><\/code> function delays a specified number of the second:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">await<\/span> asyncio.sleep(seconds)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because <code><code>sleep()<\/code><\/code> is a coroutine, you need to use the <code><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-async-await\/\">await<\/a><\/code> keyword. For example, the following uses the <code><code>sleep()<\/code><\/code> coroutine to simulate an API call:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> asyncio\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">call_api<\/span><span class=\"hljs-params\">(message, result=<span class=\"hljs-number\">1000<\/span>, delay=<span class=\"hljs-number\">3<\/span>)<\/span>:<\/span>\n    print(message)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(delay)\n    <span class=\"hljs-keyword\">return<\/span> result<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>call_api()<\/code> is a coroutine. It displays a message, pauses a specified number of seconds (default to three seconds), and returns a result.<\/p>\n\n\n\n<p>The following program uses the <code>call_api()<\/code> twice and measures the time it takes to complete:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> asyncio\n<span class=\"hljs-keyword\">import<\/span> time\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">call_api<\/span><span class=\"hljs-params\">(message, result=<span class=\"hljs-number\">1000<\/span>, delay=<span class=\"hljs-number\">3<\/span>)<\/span>:<\/span>\n    print(message)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(delay)\n    <span class=\"hljs-keyword\">return<\/span> result\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    start = time.perf_counter()\n\n    price = <span class=\"hljs-keyword\">await<\/span> call_api(<span class=\"hljs-string\">'Get stock price of GOOG...'<\/span>, <span class=\"hljs-number\">300<\/span>)\n    print(price)\n\n    price = <span class=\"hljs-keyword\">await<\/span> call_api(<span class=\"hljs-string\">'Get stock price of APPL...'<\/span>, <span class=\"hljs-number\">400<\/span>)\n    print(price)\n\n    end = time.perf_counter()\n    print(<span class=\"hljs-string\">f'It took <span class=\"hljs-subst\">{round(end-start,<span class=\"hljs-number\">0<\/span>)}<\/span> second(s) to complete.'<\/span>)\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Get stock price of GOOG...\n<span class=\"hljs-number\">300<\/span>\nGet stock price of APPL...\n<span class=\"hljs-number\">400<\/span>\nIt took <span class=\"hljs-number\">6.0<\/span> second(s) to complete.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works (focusing on the <code>main()<\/code> coroutine):<\/p>\n\n\n\n<p>First, start a timer to measure the time using the <code>perf_counter()<\/code> function of the <code>time<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"> start = time.perf_counter()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, call the <code>call_api()<\/code> coroutine and display the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">price = <span class=\"hljs-keyword\">await<\/span> call_api(<span class=\"hljs-string\">'Get stock price of GOOG...'<\/span>, <span class=\"hljs-number\">300<\/span>)\nprint(price)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, call the <code>call_api()<\/code> a second time:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">price = <span class=\"hljs-keyword\">await<\/span> call_api(<span class=\"hljs-string\">'Get stock price of APPL...'<\/span>, <span class=\"hljs-number\">400<\/span>)\nprint(price)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, show the time the program takes to complete:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">end = time.perf_counter()\nprint(<span class=\"hljs-string\">f'It took <span class=\"hljs-subst\">{round(end-start,<span class=\"hljs-number\">0<\/span>)}<\/span> second(s) to complete.'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since each <code>call_api()<\/code> takes three seconds, and calling it twice takes six seconds.<\/p>\n\n\n\n<p>In this example, we call a coroutine directly and don&#8217;t put it on the event loop to run. Instead, we get a coroutine object and use the <code>await<\/code> keyword to execute it and get a result.<\/p>\n\n\n\n<p>The following picture illustrates the execution flow of the program:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-create_task-async.svg\" alt=\"Python create_task Asynchronously\" class=\"wp-image-4128\"\/><\/figure>\n<\/div>\n\n\n<p>In other words, we use <code>async<\/code> and <code>await<\/code> to write asynchronous code but can&#8217;t run it concurrently. To run multiple operations concurrently, we&#8217;ll need to use something called tasks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-tasks'>Introduction to Python tasks <a href=\"#introduction-to-python-tasks\" class=\"anchor\" id=\"introduction-to-python-tasks\" title=\"Anchor for Introduction to Python tasks\">#<\/a><\/h2>\n\n\n\n<p>A task is a wrapper of a coroutine that schedules the coroutine to run on the event loop as soon as possible.<\/p>\n\n\n\n<p>The scheduling and execution occur in a non-blocking manner. In other words, you can create a task and execute other code instantly while the task is running.<\/p>\n\n\n\n<p>Notice that the task is different from the <code>await<\/code> keyword that blocks the entire coroutine until the operation completes with a result.<\/p>\n\n\n\n<p>It&#8217;s important that you can create multiple tasks and schedule them to run instantly on the event loop at the same time. <\/p>\n\n\n\n<p>To create a task, you pass a coroutine to the <code><code>create_task()<\/code><\/code> function of the <code>asyncio<\/code> package. The <code><code>create_task()<\/code><\/code> function returns a <code>Task<\/code> object.<\/p>\n\n\n\n<p>The following program illustrates how to create two tasks that schedule and execute the <code>call_api()<\/code> coroutine:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> asyncio\n<span class=\"hljs-keyword\">import<\/span> time\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">call_api<\/span><span class=\"hljs-params\">(message, result=<span class=\"hljs-number\">1000<\/span>, delay=<span class=\"hljs-number\">3<\/span>)<\/span>:<\/span>\n    print(message)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(delay)\n    <span class=\"hljs-keyword\">return<\/span> result\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    start = time.perf_counter()\n\n    task_1 = asyncio.create_task(\n        call_api(<span class=\"hljs-string\">'Get stock price of GOOG...'<\/span>, <span class=\"hljs-number\">300<\/span>)\n    )\n\n    task_2 = asyncio.create_task(\n        call_api(<span class=\"hljs-string\">'Get stock price of APPL...'<\/span>, <span class=\"hljs-number\">300<\/span>)\n    )\n\n    price = <span class=\"hljs-keyword\">await<\/span> task_1\n    print(price)\n\n    price = <span class=\"hljs-keyword\">await<\/span> task_2\n    print(price)\n\n    end = time.perf_counter()\n    print(<span class=\"hljs-string\">f'It took <span class=\"hljs-subst\">{round(end-start,<span class=\"hljs-number\">0<\/span>)}<\/span> second(s) to complete.'<\/span>)\n\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Get stock price of GOOG...\nGet stock price of APPL...\n<span class=\"hljs-number\">300<\/span>\n<span class=\"hljs-number\">300<\/span>\nIt took <span class=\"hljs-number\">3.0<\/span> second(s) to complete.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, start a timer:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">start = time.perf_counter()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, create a task and schedule it to run on the event loop immediately:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">task_1 = asyncio.create_task(\n   call_api(<span class=\"hljs-string\">'Get stock price of GOOG...'<\/span>, <span class=\"hljs-number\">300<\/span>)\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, create another task and schedule it to run on the event loop immediately:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">task_2 = asyncio.create_task(\n    call_api(<span class=\"hljs-string\">'Get stock price of APPL...'<\/span>, <span class=\"hljs-number\">400<\/span>)\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>After that, wait for the tasks to be completed:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">price = <span class=\"hljs-keyword\">await<\/span> task_1\nprint(price)\n\nprice = <span class=\"hljs-keyword\">await<\/span> task_2\nprint(price)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;s important to use the <code>await<\/code> keyword to wait for the tasks at some point in the program. <\/p>\n\n\n\n<p>If we did not use the <code>await<\/code> keyword, Python would schedule the task to run but stopped it when the <code>asyncio.run()<\/code> shutdown the event loop.<\/p>\n\n\n\n<p>The following picture illustrates the execution flow of the program:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-create_task-concurrency.svg\" alt=\"Python create_task Concurrency\" class=\"wp-image-4129\"\/><\/figure>\n\n\n\n<p>Finally, show the time it takes to complete the <code>main()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">end = time.perf_counter()\nprint(<span class=\"hljs-string\">f'It took <span class=\"hljs-subst\">{round(end-start,<span class=\"hljs-number\">0<\/span>)}<\/span> second(s) to complete.'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By using the <code>create_task()<\/code> function, the program is much faster. The more tasks you run, the faster it is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='running-other-tasks-while-waiting'>Running other tasks while waiting <a href=\"#running-other-tasks-while-waiting\" class=\"anchor\" id=\"running-other-tasks-while-waiting\" title=\"Anchor for Running other tasks while waiting\">#<\/a><\/h2>\n\n\n\n<p>When the <code>call_api<\/code> is running, you can run other tasks. For example, the following program displays a message every second while waiting for the <code>call_api<\/code> tasks:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> asyncio\n<span class=\"hljs-keyword\">import<\/span> time\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">call_api<\/span><span class=\"hljs-params\">(message, result=<span class=\"hljs-number\">1000<\/span>, delay=<span class=\"hljs-number\">3<\/span>)<\/span>:<\/span>\n    print(message)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(delay)\n    <span class=\"hljs-keyword\">return<\/span> result\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">show_message<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> _ <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">3<\/span>):\n        <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n        print(<span class=\"hljs-string\">'API call is in progress...'<\/span>)\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    start = time.perf_counter()\n\n    message_task = asyncio.create_task(\n        show_message()\n    )\n\n    task_1 = asyncio.create_task(\n        call_api(<span class=\"hljs-string\">'Get stock price of GOOG...'<\/span>, <span class=\"hljs-number\">300<\/span>)\n    )\n\n    task_2 = asyncio.create_task(\n        call_api(<span class=\"hljs-string\">'Get stock price of APPL...'<\/span>, <span class=\"hljs-number\">300<\/span>)\n    )\n\n    price = <span class=\"hljs-keyword\">await<\/span> task_1\n    print(price)\n\n    price = <span class=\"hljs-keyword\">await<\/span> task_2\n    print(price)\n\n    <span class=\"hljs-keyword\">await<\/span> message_task\n\n    end = time.perf_counter()\n    print(<span class=\"hljs-string\">f'It took <span class=\"hljs-subst\">{round(end-start,<span class=\"hljs-number\">0<\/span>)}<\/span> second(s) to complete.'<\/span>)\n\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Get stock price of GOOG...\nGet stock price of APPL...\nAPI call <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">in<\/span> progress...\nAPI call <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">in<\/span> progress...\n<span class=\"hljs-number\">300<\/span>\n<span class=\"hljs-number\">300<\/span>\nAPI call <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">in<\/span> progress...\nIt took <span class=\"hljs-number\">3.0<\/span> second(s) to complete.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture illustrates the execution flow:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-create_task-running-other-tasks.svg\" alt=\"\" class=\"wp-image-4130\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A task is a wrapper of a coroutine that schedules the coroutine to run on the event loop as soon as possible.<\/li>\n\n\n\n<li>Use the <code>create_task()<\/code> function of the <code>asyncio<\/code> library to create a task.<\/li>\n\n\n\n<li>Use the <code>await<\/code> keyword with the task at some point in the program so that the task can be completed before the event loop is closed by the <code>asyncio.run()<\/code> function.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4124\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-create_task\/\"\n\t\t\t\tdata-post-title=\"Python asyncio.create_task()\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4124\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-create_task\/\"\n\t\t\t\tdata-post-title=\"Python asyncio.create_task()\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the create_task function of the asyncio package to run multiple tasks concurrently.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4104,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4124","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4124"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4124\/revisions"}],"predecessor-version":[{"id":7372,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4124\/revisions\/7372"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4104"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}