title: Making asyncio gather wait until all tasks are done
body: <p>I have functions that are supposed to run in paraller and gather data for a second, and then return the data once the time has passed.</p>
  
  <p>Problem is, one task (to my understanding) finishes before the other, causing <code>results</code> to be empty on that result. Below code for example:</p>
  
  <pre class="lang-py prettyprint-override"><code>import asyncio
  import time
  
  async def task(id, end_time):
      print('Started task ', id)
      results = []
      while time.time() &lt; end_time:
          results.append(1)
      return results
  
  
  async def main():
      while True:
          end_time = time.time() + 1
          results = await asyncio.gather(*[task(i,end_time) for i in range(2)])
          print(len(results[0]), len(results[1]))
  try:
      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())
  except KeyboardInterrupt:
      pass
  finally:
      loop.close()
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>Started task  0
  Started task  1
  5580452 0
  </code></pre>
  
  <p>Where am I going wrong? I know I should somehow make it wait for all tasks to be finished before doing things with the results, but don't really know how to achieve this, after a while of fiddling around with different things.</p>