title: Python Asyncio - RuntimeError: Cannot close a running event loop
body: <p>I'm trying to resolve this error: <code>RuntimeError: Cannot close a running event loop</code> in my asyncio process. I believe it's happening because there's a failure while tasks are still pending, and then I try to close the event loop. I'm thinking I need to await the remaining responses prior to closing the event loop, but I'm not sure how to accomplish that correctly in my specific situation.</p>
  
  <pre><code> def start_job(self):
  
          if self.auth_expire_timestamp &lt; get_timestamp():
              api_obj = api_handler.Api('Api Name', self.dbObj)
              self.api_auth_resp = api_obj.get_auth_response()
              self.api_attr = api_obj.get_attributes()
  
  
          try:
              self.queue_manager(self.do_stuff(json_data))
          except aiohttp.ServerDisconnectedError as e:
              logging.info("Reconnecting...")
              api_obj = api_handler.Api('API Name', self.dbObj)
              self.api_auth_resp = api_obj.get_auth_response()
              self.api_attr = api_obj.get_attributes()
              self.run_eligibility()
  
  async def do_stuff(self, data):
  
      tasks = []
  
      async with aiohttp.ClientSession() as session:
          for row in data:
              task = asyncio.ensure_future(self.async_post('url', session, row))
              tasks.append(task)
          result = await asyncio.gather(*tasks)
      self.load_results(result)
  
  
  def queue_manager(self, method):
      self.loop = asyncio.get_event_loop()
      future = asyncio.ensure_future(method)
      self.loop.run_until_complete(future)
  
  
  async def async_post(self, resource, session, data):
          async with session.post(self.api_attr.api_endpoint + resource, headers=self.headers, data=data) as response:
              resp = []
              try:
                  headers = response.headers['foo']
                  content = await response.read()
                  resp.append(headers)
                  resp.append(content)
              except KeyError as e:
                  logging.error('KeyError at async_post response')
                  logging.error(e)
          return resp
  
  
  def shutdown(self):
      //need to do something here to await the remaining tasks and then I need to re-start a new event loop, which i think i can do, just don't know how to appropriately stop the current one.
      self.loop.close() 
      return True
  </code></pre>
  
  <p>How can I handle the error and properly close the event loop so I can start a new one and essentially re-boot the whole program and continue on.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This is what I'm trying now, based on <a href="https://stackoverflow.com/a/30766124/4113027">this SO answer</a>. Unfortunately, this error only happens rarely, so unless I can force it, i will have to wait and see if it works. In my <code>queue_manager</code> method I changed it to this:</p>
  
  <pre><code>try:
     self.loop.run_until_complete(future)
  except Exception as e:
     future.cancel()
     self.loop.run_until_complete(future)
     future.exception()
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I got rid of the <code>shutdown()</code> method and added this to my <code>queue_manager()</code> method instead and it seems to be working without issue:  </p>
  
  <pre><code>  try:
          self.loop.run_until_complete(future)
      except Exception as e:
          future.cancel()
          self.check_in_records()
          self.reconnect()
          self.start_job()
          future.exception()
  </code></pre>