title: Make a Python asyncio call from a Flask route
body: <p>I want to execute an async function every time the Flask route is executed. Why is  the <code>abar</code> function never executed?</p>
  
  <pre><code>import asyncio
  from flask import Flask
  
  async def abar(a):
      print(a)
  
  loop = asyncio.get_event_loop()
  app = Flask(__name__)
  
  @app.route("/")
  def notify():
      asyncio.ensure_future(abar("abar"), loop=loop)
      return "OK"
  
  if __name__ == "__main__":
      app.run(debug=False, use_reloader=False)
      loop.run_forever()
  </code></pre>
  
  <p>I also tried putting the blocking call in a separate thread. But it is still not calling the <code>abar</code> function. </p>
  
  <pre><code>import asyncio
  from threading import Thread
  from flask import Flask
  
  async def abar(a):
      print(a)
  
  app = Flask(__name__)
  
  def start_worker(loop):
      asyncio.set_event_loop(loop)
      try:
          loop.run_forever()
      finally:
          loop.close()
  
  worker_loop = asyncio.new_event_loop()
  worker = Thread(target=start_worker, args=(worker_loop,))
  
  @app.route("/")
  def notify():
      asyncio.ensure_future(abar("abar"), loop=worker_loop)
      return "OK"
  
  if __name__ == "__main__":
      worker.start()
      app.run(debug=False, use_reloader=False)
  </code></pre>