{"id":986,"hash":"aec4dde72cc9c339315371f77503b71f4c5d33f9f063d250a67da9596ae51d7f","pattern":"&quot;RuntimeError: This event loop is already running&quot;; debugging aiohttp, asyncio and IDE &quot;spyder3&quot; in python 3.6.5","full_message":"I'm struggling to understand why I am getting the \"RuntimeError: This event loop is already running\" runtime error. I have tried to run snippets of code from \"https://aiohttp.readthedocs.io/en/stable/\" however, I keep getting the same issue. \n\nCode snippet from Tutorial:\n\nimport aiohttp\nimport asyncio\nimport async_timeout\n\nasync def fetch(session, url):\n    async with async_timeout.timeout(10):\n        async with session.get(url) as response:\n            return await response.text()\n\nasync def main():\n    async with aiohttp.ClientSession() as session:\n        html = await fetch(session, 'http://python.org')\n        print(html)\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(main())\n\nRESULTS from tutorial snippet (while running code from spyder IDE):\n\nRuntimeError: This event loop is already running\n\n<!doctype html>\"\n\n... (more html)\n\nPersonal code snippet (not from the tutorial referenced above):\n\nimport aiohttp\nimport asyncio\nimport time\n\nurls = ['https://api.robinhood.com/quotes/c4f6720a-0364-4e7b-8c41-705696759e1a/']\n\nasync def fetch(client, url):\n    async with client.request('get', url) as response:\n        if response.status == 200:\n            data = await response.text()\n        else:\n            data = []\n        print(data)\n        return(data)\n\nasync def get_async_urls(urls):\n    async with aiohttp.ClientSession() as client:\n        return await asyncio.gather(*(fetch(client, url) for url in urls))\n\nif __name__ == '__main__':\n    t0 = time.time()\n    loop = asyncio.get_event_loop()\n    results = loop.run_until_complete(get_async_urls(urls))\n    print(results)\n    t1 = time.time()\n    total_time = t1-t0\n    loop.close()\n\nRESULTS from personal snippet (while running code from spyder IDE):\n\nRuntimeError: This event loop is already running\n\n{\"ask_price\":\"14.9000\",\"ask_size\":100,\"bid_price\":\"14.0100\",\"bid_size\":100,\"last_trade_price\":\"14.7900\",\"last_extended_hours_trade_price\":\"14.7900\",\"previous_close\":\"14.3600\",\"adjusted_previous_close\":\"14.3600\",\"previous_close_date\":\"2018-05-07\",\"symbol\":\"SURF\",\"trading_halted\":false,\"has_traded\":true,\"last_trade_price_source\":\"consolidated\",\"updated_at\":\"2018-05-08T20:01:21Z\",\"instrument\":\"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/\"}\n\nRESULTS from personal snippet (while running from cmd \"python personal_snippet.py\"):\n\n{\"ask_price\":\"14.9000\",\"ask_size\":100,\"bid_price\":\"14.0100\",\"bid_size\":100,\"last_trade_price\":\"14.7900\",\"last_extended_hours_trade_price\":\"14.7900\",\"previous_close\":\"14.3600\",\"adjusted_previous_close\":\"14.3600\",\"previous_close_date\":\"2018-05-07\",\"symbol\":\"SURF\",\"trading_halted\":false,\"has_traded\":true,\"last_trade_price_source\":\"consolidated\",\"updated_at\":\"2018-05-08T20:01:21Z\",\"instrument\":\"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/\"}\n['{\"ask_price\":\"14.9000\",\"ask_size\":100,\"bid_price\":\"14.0100\",\"bid_size\":100,\"last_trade_price\":\"14.7900\",\"last_extended_hours_trade_price\":\"14.7900\",\"previous_close\":\"14.3600\",\"adjusted_previous_close\":\"14.3600\",\"previous_close_date\":\"2018-05-07\",\"symbol\":\"SURF\",\"trading_halted\":false,\"has_traded\":true,\"last_trade_price_source\":\"consolidated\",\"updated_at\":\"2018-05-08T20:01:21Z\",\"instrument\":\"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/\"}']\n\nThe above results appear to point to the issue being related to the Spyder IDE. \n\nI have two questions:\n\nWhy am I getting this error? \nIt seems like other people do not get this error when running the tutorial code. (Potentially answered: POSSIBLE BUG IN SPYDER3)\n\nThis seems to only happen in the spyder IDE. I ran both snippets of code in from the cmd prompt and no error appeared. Thanks to @MikhailGerasimov for the suggestion. \n\nGiven that I have two print commands (in the second snippet of code) and that only one set of \"data\" was printed, then why is data not getting back to the original call (results = loop.run_until_complete(get_async_urls(urls))) (Potentially answered: POSSIBLE BUG IN SPYDER3)\n\nThis seems to only happen in the spyder IDE. I ran the second snippet of code in from the cmd prompt and both prints appeared. Thanks to @MikhailGerasimov for the suggestion.\n\nBOTH QUESTIONS HAVE (probably) BEEN ANSWERED. I will reference this question when I submit an issue to spyder IDE. I will continue to update this as things evolve on the spyder side of things. If they come back and say the issue does not lie with them, then I will reopen the questions in a slightly different format. \n\nThanks for the help!\n\nLink to the referenced issue submission in Spyder IDE's github:\nhttps://github.com/spyder-ide/spyder/issues/7096","ecosystem":"pypi","package_name":"spyder","package_version":null,"solution":"I have the same issue with Spyder, The only solution that worked for me was to use nest_asyncio \n\ninstall the nest_asyncio by using the command\n\npip install nest_asyncio\n\nAdd the below lines in your file\n\nimport nest_asyncio\nnest_asyncio.apply()\n\nAnd the issue must be fixed.\n\nFrom the docs's \n\n  By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it’s impossible to run tasks and wait for the result. Trying to do so will give the error “RuntimeError: This event loop is already running”.\n\n  \n  The issue pops up in various environments, such as web servers, GUI\n  applications and in Jupyter notebooks.\n\n  \n  This module patches asyncio to allow nested use of asyncio.run and\n  loop.run_until_complete.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/50243393/runtimeerror-this-event-loop-is-already-running-debugging-aiohttp-asyncio-a","votes":11,"created_at":"2026-04-19T04:52:07.357759+00:00","updated_at":"2026-04-19T04:52:07.357759+00:00"}