{"id":987,"hash":"8221e5364e08caf19c6c416af6092acc49a9f3f6d2ec158c46ae384d3711983d","pattern":"asyncio throws runtime error with exception ignored","full_message":"Below is a simple program to collect URL lengths.\n\nimport aiohttp\nimport asyncio\nfrom time import perf_counter\n\nURLS = ['http://www.cnn.com', 'http://www.huffpost.com', 'http://europe.wsj.com',\n        'http://www.bbc.co.uk', 'http://failfailfail.com']\n\nasync def async_load_url(url, session):\n    try:\n        async with session.get(url) as resp:\n            content = await resp.read()\n        print(f\"{url!r} is {len(content)} bytes\")\n    except IOError:\n        print(f\"failed to load {url}\")        \n\nasync def main():\n\n    async with aiohttp.ClientSession() as session:\n        tasks = [async_load_url(url, session) for url in URLS]\n        await asyncio.wait(tasks)\n\nif __name__ == \"__main__\":\n    start = perf_counter()\n    asyncio.run(main())\n    elapsed = perf_counter() - start\n    print(f\"\\nTook {elapsed} seconds\")\n\nWhy is the following code failing with a runtime error with exception ignored in python 3.9? How to fix it?\n\nTraceback is: RuntimeError: Event loop is closed specifically with Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001F8A7A713A0>","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"This is caused by a known issue in aiohttp on Windows, for details check out the bug at https://github.com/aio-libs/aiohttp/issues/4324\n\nThere are a couple of hacks that silence this error. The first way is to get the event loop and call run_until_complete instead of asyncio.run(main()) like so:\n\nasyncio.get_event_loop().run_until_complete(main())\n\nAlternatively, changing the event loop policy to WindowsSelectorEventLoopPolicy before calling asyncio.run(main()) also works as the problem seems to occur when using WindowsProtractorEventLoopPolicy.\n\nasyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())\nasyncio.run(main())\n\nOf course, the second solution will make your code platform specific, so be careful.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/68123296/asyncio-throws-runtime-error-with-exception-ignored","votes":11,"created_at":"2026-04-19T04:52:07.358282+00:00","updated_at":"2026-04-19T04:52:07.358282+00:00"}