title: asyncio loop.run_until_complete finishes before all the results have returned from asyncio.gather with loop.run_in_executor
body: <p>I have a program which makes async queries to a graphql system, then writes those results to file. I'm calling the main function with <code>loop.run_until_complete</code>.</p>
  <pre class="lang-py prettyprint-override"><code>       try:
             loop.run_until_complete(run_suite(gql_client))
             log.info('Done.')
         except asyncio.CancelledError:
             done
  </code></pre>
  <p>Later in <code>run_suite</code>, I write the results of the various queries to file (in <code>run_query</code>). The function that writes the files is blocking (<code>write_query_result</code>), so I call it with <code>run_in_executor</code>.</p>
  <pre class="lang-py prettyprint-override"><code>def write_query_result(folder: str, filename: str, result: Any):
      dirname = '%s/%s' % (folder, filename)
      log.info('Writing [%s]' % dirname)
      writer.write_data(dirname, result)
      return result
  
  async def run_query(query: List[Dict[str, Any]]) -&gt; Union[asyncio.Future, List[asyncio.Future], Exception]:
      _loop = asyncio.get_event_loop()
      try:
          result = await asyncio.create_task(query['func']())
          writers = []
          for fname in result.keys():
              result = await _loop.run_in_executor(None, write_query_result, query['folder'], fname+query['file_extension'], result)
              writers.append(result)
          return writers
      except Exception as e:
          log.error('Error getting results. Query [%s]. Error [%s]' % (
              query['name'], e))
          raise e
  
  async def run_queries(session: aiohttp.ClientSession, client_id: str, host: str = '127.0.0.1') -&gt; asyncio.Future:
  ...
      tasks = []
      for query_set in queries:
          for query in query_set:
              tasks.append(asyncio.create_task(run_query(query)))
  
      return await asyncio.gather(*tasks)
  
  </code></pre>
  <p>With this code my queries run asyncronously. The files get written asyncronously in the executor, and then when all the files have been written the loop completes. I see it log the 'Done' message and the program exits.</p>
  <p>However, I noticed that if I modify the code for writing the results as shown below (gathering the results of all calls to <code>run_in_executor</code>), then my loop actually completes before all the files are written. I see the 'Done' message, and then I still see it printing that its writing to file.</p>
  <pre class="lang-py prettyprint-override"><code>async def run_query(query: List[Dict[str, Any]]) -&gt; Union[asyncio.Future, List[asyncio.Future], Exception]:
      _loop = asyncio.get_event_loop()
      try:
          result = await asyncio.create_task(query['func']())
          writers = []
          for fname in result.keys():
              writers.append(_loop.run_in_executor(None, write_query_result, query['folder'], fname+query['file_extension'], result))
          return asyncio.gather(*writers)
      except Exception as e:
          log.error('Error getting results. Query [%s]. Error [%s]' % (
              query['name'], e))
          raise e
  </code></pre>
  <p>I'm fairly new to asyncio and am not 100% sure what might be going on. I'd appreciate any insight on why this might be happening.</p>