title: How do I return multiple values from an async function to a synchronous context?
body: <p>I need to gather results from async calls and return them to an ordinary function--bridging the async and sync code sections confuses me.</p>
  
  <p>First try, with an ugly inout param.</p>
  
  <pre><code>import asyncio
  import aiohttp
  
  async def one_call(url):
      async with aiohttp.ClientSession() as session:
          async with session.get(url) as response:
              txt = await response.text()
              return txt[0:20]
  
  
  async def do_all(result_inout):
      urls = ["https://cnn.com", "https://nyt.com", "http://reuters.com"]
      out = await asyncio.gather(*[one_call(url) for url in urls])
      result_inout += out
  
  if __name__ == "__main__":
      result_inout = []
      asyncio.run(do_all(result_inout))
  
      print(result_inout)
  </code></pre>
  
  <p>Second try,  but with direct use of the event loop, which is <a href="https://docs.python.org/3/library/asyncio-eventloop.html" rel="nofollow noreferrer">discouraged</a> for application code.</p>
  
  <pre><code>if __name__ == "__main__":
      # same imports, same one_call, same urls
      loop = asyncio.get_event_loop()
  
      aggregate_future = asyncio.gather(*[one_call(url) for url in urls])
      results = loop.run_until_complete(aggregate_future)
      loop.close()
  
      print(results) 
  </code></pre>
  
  <p>What is the best way to do this?</p>