{"id":955,"hash":"6b8f8415d317fce5c06a752f7d08525441f45077f6ebc1a2aee8b8ed1e247e61","pattern":"Learning asyncio: &quot;coroutine was never awaited&quot; warning error","full_message":"I am trying to learn to use asyncio in Python to optimize scripts.\nMy example returns a coroutine was never awaited warning, can you help to understand and find how to solve it? \n\nimport time \nimport datetime\nimport random\nimport asyncio\n\nimport aiohttp\nimport requests\n\ndef requete_bloquante(num):\n    print(f'Get {num}')\n    uid = requests.get(\"https://httpbin.org/uuid\").json()['uuid']\n    print(f\"Res {num}: {uid}\")\n\ndef faire_toutes_les_requetes():\n    for x in range(10):\n        requete_bloquante(x)\n\nprint(\"Bloquant : \")\nstart = datetime.datetime.now()\nfaire_toutes_les_requetes()\nexec_time = (datetime.datetime.now() - start).seconds\nprint(f\"Pour faire 10 requêtes, ça prend {exec_time}s\\n\")\n\nasync def requete_sans_bloquer(num, session):\n    print(f'Get {num}')\n    async with session.get(\"https://httpbin.org/uuid\") as response:\n        uid = (await response.json()['uuid'])\n    print(f\"Res {num}: {uid}\")\n\nasync def faire_toutes_les_requetes_sans_bloquer():\n    loop = asyncio.get_event_loop()\n    with aiohttp.ClientSession() as session:\n        futures = [requete_sans_bloquer(x, session) for x in range(10)]\n        loop.run_until_complete(asyncio.gather(*futures))\n    loop.close()\n    print(\"Fin de la boucle !\")\n\nprint(\"Non bloquant : \")\nstart = datetime.datetime.now()\nfaire_toutes_les_requetes_sans_bloquer()\nexec_time = (datetime.datetime.now() - start).seconds\nprint(f\"Pour faire 10 requêtes, ça prend {exec_time}s\\n\")\n\nThe first classic part of the code runs correctly, but the second half only produces: \n\nsynchronicite.py:43: RuntimeWarning: coroutine 'faire_toutes_les_requetes_sans_bloquer' was never awaited","ecosystem":"pypi","package_name":"python-asyncio","package_version":null,"solution":"You made faire_toutes_les_requetes_sans_bloquer an awaitable function, a coroutine, by using async def.\n\nWhen you call an awaitable function, you create a new coroutine object. The code inside the function won't run until you then await on the function or run it as a task:\n\n>>> async def foo():\n...     print(\"Running the foo coroutine\")\n...\n>>> foo()\n<coroutine object foo at 0x10b186348>\n>>> import asyncio\n>>> asyncio.run(foo())\nRunning the foo coroutine\n\nYou want to keep that function synchronous, because you don't start the loop until inside that function:\n\ndef faire_toutes_les_requetes_sans_bloquer():\n    loop = asyncio.get_event_loop()\n    # ...\n    loop.close()\n    print(\"Fin de la boucle !\")\n\nHowever, you are also trying to use a aiohttp.ClientSession() object, and that's an asynchronous context manager, you are expected to use it with async with, not just with, and so has to be run in aside an awaitable task. If you use with instead of async with a TypeError(\"Use async with instead\") exception will be raised.\n\nThat all means you need to move the loop.run_until_complete() call out of your faire_toutes_les_requetes_sans_bloquer() function, so you can keep that as the main task to be run; you can call and await on asycio.gather() directly then:\n\nasync def faire_toutes_les_requetes_sans_bloquer():\n    async with aiohttp.ClientSession() as session:\n        futures = [requete_sans_bloquer(x, session) for x in range(10)]\n        await asyncio.gather(*futures)\n    print(\"Fin de la boucle !\")\n\nprint(\"Non bloquant : \")\nstart = datetime.datetime.now()\nasyncio.run(faire_toutes_les_requetes_sans_bloquer())\nexec_time = (datetime.datetime.now() - start).seconds\nprint(f\"Pour faire 10 requêtes, ça prend {exec_time}s\\n\")\n\nI used the new asyncio.run() function (Python 3.7 and up) to run the single main task. This creates a dedicated loop for that top-level coroutine and runs it until complete.\n\nNext, you need to move the closing ) parenthesis on the await resp.json() expression:\n\nuid = (await response.json())['uuid']\n\nYou want to access the 'uuid' key on the result of the await, not the coroutine that response.json() produces.\n\nWith those changes your code works, but the asyncio version finishes in sub-second time; you may want to print microseconds:\n\nexec_time = (datetime.datetime.now() - start).total_seconds()\nprint(f\"Pour faire 10 requêtes, ça prend {exec_time:.3f}s\\n\")\n\nOn my machine, the synchronous requests code in about 4-5 seconds, and the asycio code completes in under .5 seconds.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/54441424/learning-asyncio-coroutine-was-never-awaited-warning-error","votes":130,"created_at":"2026-04-19T04:52:05.807417+00:00","updated_at":"2026-04-19T04:52:07.346418+00:00"}