title: Python call callback after async function is done
body: <p>I'm trying to call a callback once a async function is done running</p>
  
  <p>Here's an example of what im trying to do:</p>
  
  <pre class="lang-py prettyprint-override"><code>import asyncio
  
  async def asyncfunction():
      print('Hello')
      await asyncio.sleep(10)
      print('World')
      return 10
  
  def callback(n):
      print(f'The async function returned: {n}')
  
  loop = asyncio.get_event_loop()
  
  # Will block the print until everything is done
  callback(loop.run_until_complete(asyncfunction()))
  print('Hey')
  </code></pre>
  
  <p>Here's what that does:</p>
  
  <pre><code>Hello
  World
  The async function returned: 10
  Hey
  </code></pre>
  
  <p>And here's what I want it to do<br>
  Edit: The position of the 'Hey' doesn't really matter, as long as it doesn't have to wait for the async function to be done</p>
  
  <pre><code>Hello
  Hey
  World
  The async function returned: 10
  </code></pre>
  
  <p>Edit: after some testing I have found a way that does what I want, although I don't know if its the best way to do it</p>
  
  <pre class="lang-py prettyprint-override"><code>import asyncio
  import threading
  
  async def asyncfunction():
      print('Hello')
      await asyncio.sleep(10)
      print('World')
      return 10
  
  def callback(n):
      print(f'The async function returned: {n}')
  
  def wrapper(loop):
      callback(loop.run_until_complete(asyncfunction()))
  
  loop = asyncio.get_event_loop()
  thr = threading.Thread(target=wrapper,args=(loop,))
  thr.start()
  print('Hey')
  </code></pre>