{"flag":true,"single":true,"pageTitle":"Async (asyncio) await in Python","post":{"id":342,"user_id":"1","slug":"async-await-in-python-0b9z","title":"Async (asyncio) await in Python","body":"<p>asyncio lets your program do multiple tasks at the same time without waiting.<\/p>\r\n<pre class=\"language-markup\"><code>import asyncio\r\nasync def hello():\r\n    print(\"Hello\")\r\n    await asyncio.sleep(1)\r\n    print(\"World\")\r\nasyncio.run(hello())<\/code><\/pre>\r\n<p><strong>Multiple tasks:<\/strong><\/p>\r\n<p>gather accept multiple functions-&gt; run all at same time, -&gt; wait for finish all -&gt;return results if any<br><strong>ie <\/strong>Order 2 dishes and wait at table ???????<\/p>\r\n<pre class=\"language-markup\"><code>import asyncio\r\n\r\nasync def task1():\r\n    await asyncio.sleep(2)\r\n    print(\"Task 1 done\")\r\n\r\nasync def task2():\r\n    await asyncio.sleep(1)\r\n    print(\"Task 2 done\")\r\n\r\nasync def main():\r\n    await asyncio.gather(task1(), task2())\r\n\r\nasyncio.run(main())<\/code><\/pre>\r\n<p>output:<\/p>\r\n<pre class=\"language-markup\"><code>Task 2 done\r\nTask 1 done<\/code><\/pre>\r\n<p><strong>Creating Tasks: runs in background<br><\/strong>Starts task in background immediately<br><strong>ie<\/strong> Order food, then go do shopping ????, come back later<\/p>\r\n<pre class=\"language-ruby\"><code>async def work():\r\n    await asyncio.sleep(2)\r\n    print(\"Work done\")\r\n\r\nasync def main():\r\n    task = asyncio.create_task(work())\r\n    print(\"Doing other things...\")\r\n    await task\r\n\r\nasyncio.run(main())<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<table style=\"border-collapse: collapse; width: 100.022%;\" border=\"1\"><colgroup><col style=\"width: 49.9084%;\"><col style=\"width: 49.9084%;\"><\/colgroup>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<p><strong>code:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import asyncio\r\n\r\nasync def work(i):\r\n    print(f\"Start {i}\")\r\n    await asyncio.sleep(2)\r\n    print(f\"End {i}\")\r\n\r\nasync def main():\r\n    await asyncio.gather(*(work(i) for i in range(5)))\r\n\r\nasyncio.run(main())<\/code><\/pre>\r\n<\/td>\r\n<td>\r\n<p><strong>output:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Start 0\r\nStart 1\r\nStart 2\r\nStart 3\r\nStart 4\r\nEnd 0\r\nEnd 1\r\nEnd 2\r\nEnd 3\r\nEnd 4<\/code><\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p><strong>Fetch multiple urls:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import asyncio\r\nimport aiohttp\r\n\r\nasync def fetch(url):\r\n    async with aiohttp.ClientSession() as session:\r\n        async with session.get(url) as response:\r\n            return await response.text()\r\n\r\nasync def main():\r\n    urls = [\r\n        \"https:\/\/example.com\",\r\n        \"https:\/\/google.com\"\r\n    ]\r\n    \r\n    tasks = [fetch(url) for url in urls]\r\n    results = await asyncio.gather(*tasks)\r\n\r\n    print(\"Fetched\", len(results), \"pages\")\r\n\r\nasyncio.run(main())<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Semaphore: ???? Only N tasks run at once<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>sem = asyncio.Semaphore(2)\r\n\r\nasync def work(i):\r\n    async with sem:\r\n        print(f\"Task {i} running\")\r\n        await asyncio.sleep(2)\r\n\r\nasync def main():\r\n    await asyncio.gather(*(work(i) for i in range(5)))\r\n\r\nasyncio.run(main())<\/code><\/pre>\r\n<p><strong>Queue:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import asyncio\r\n\r\nqueue = asyncio.Queue()\r\n\r\nasync def producer():\r\n    for i in range(5):\r\n        await queue.put(i)\r\n        print(\"Produced\", i)\r\n\r\nasync def consumer():\r\n    while True:\r\n        item = await queue.get()\r\n        print(\"Consumed\", item)\r\n        queue.task_done()\r\n\r\nasync def main():\r\n    asyncio.create_task(consumer())\r\n    await producer()\r\n    await queue.join()\r\n\r\nasyncio.run(main())<\/code><\/pre>","category_id":"5","is_private":"0","created_at":"2026-04-08T00:35:30.000000Z","updated_at":"2026-04-08T00:49:27.000000Z","category":{"id":5,"user_id":"1","name":"Python","slug":"python-6gqr","parent_id":null,"created_at":"2023-03-16T12:36:56.000000Z","updated_at":"2023-03-16T12:36:56.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"asyncio lets your program do multiple tasks at the same time without waiting. import asyncio async def hello():     print(\"Hello\")     await - Async (asyncio) await in Python (Updated: April 8, 2026) - Read more about Async (asyncio) await in Python at my programming site [SITE]","categories":[]}