title: "RuntimeError: This event loop is already running"; debugging aiohttp, asyncio and IDE "spyder3" in python 3.6.5
body: <p>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 "<a href="https://aiohttp.readthedocs.io/en/stable/" rel="noreferrer">https://aiohttp.readthedocs.io/en/stable/</a>" however, I keep getting the same issue. </p>
  
  <p><strong>Code snippet from Tutorial:</strong></p>
  
  <hr>
  
  <pre><code>import aiohttp
  import asyncio
  import async_timeout
  
  async def fetch(session, url):
      async with async_timeout.timeout(10):
          async with session.get(url) as response:
              return await response.text()
  
  async def main():
      async with aiohttp.ClientSession() as session:
          html = await fetch(session, 'http://python.org')
          print(html)
  
  loop = asyncio.get_event_loop()
  loop.run_until_complete(main())
  </code></pre>
  
  <hr>
  
  <p><strong>RESULTS from tutorial snippet (while running code from spyder IDE):</strong></p>
  
  <hr>
  
  <p>RuntimeError: This event loop is already running</p>
  
  <pre><code>&lt;!doctype html&gt;"
  </code></pre>
  
  <p>... (more html)</p>
  
  <hr>
  
  <p><strong>Personal code snippet (not from the tutorial referenced above):</strong></p>
  
  <hr>
  
  <pre><code>import aiohttp
  import asyncio
  import time
  
  urls = ['https://api.robinhood.com/quotes/c4f6720a-0364-4e7b-8c41-705696759e1a/']
  
  async def fetch(client, url):
      async with client.request('get', url) as response:
          if response.status == 200:
              data = await response.text()
          else:
              data = []
          print(data)
          return(data)
  
  async def get_async_urls(urls):
      async with aiohttp.ClientSession() as client:
          return await asyncio.gather(*(fetch(client, url) for url in urls))
  
  if __name__ == '__main__':
      t0 = time.time()
      loop = asyncio.get_event_loop()
      results = loop.run_until_complete(get_async_urls(urls))
      print(results)
      t1 = time.time()
      total_time = t1-t0
      loop.close()
  </code></pre>
  
  <hr>
  
  <p><strong>RESULTS from personal snippet (while running code from spyder IDE):</strong></p>
  
  <hr>
  
  <p>RuntimeError: This event loop is already running</p>
  
  <p>{"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":"<a href="https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/" rel="noreferrer">https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/</a>"}</p>
  
  <hr>
  
  <p><strong>RESULTS from personal snippet (while running from cmd "python personal_snippet.py"):</strong></p>
  
  <hr>
  
  <p>{"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":"<a href="https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/" rel="noreferrer">https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/</a>"}
  ['{"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":"<a href="https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/" rel="noreferrer">https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/</a>"}']</p>
  
  <hr>
  
  <p><strong>The above results appear to point to the issue being related to the Spyder IDE.</strong> </p>
  
  <hr>
  
  <p><strong>I have two questions:</strong></p>
  
  <ol>
  <li><p><strong>Why am I getting this error?</strong> 
  It seems like other people do not get this error when running the tutorial code. (Potentially answered: POSSIBLE BUG IN SPYDER3)</p>
  
  <p><em>This 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.</em> </p></li>
  <li><p>Given that I have two print commands (in the second snippet of code) and that only one set of "data" was printed, then <strong>why is data not getting back to the original call (results = loop.run_until_complete(get_async_urls(urls)))</strong> (Potentially answered: POSSIBLE BUG IN SPYDER3)</p>
  
  <p><em>This 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.</em></p></li>
  </ol>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <p>BOTH 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. </p>
  
  <p>Thanks for the help!</p>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <p>Link to the referenced issue submission in Spyder IDE's github:
  <a href="https://github.com/spyder-ide/spyder/issues/7096" rel="noreferrer">https://github.com/spyder-ide/spyder/issues/7096</a></p>
  
  <hr>
  
  <hr>
  
  <hr>