title: How to cleanly exit a python async app with executor tasks still waiting for console input
body: <p>I'm trying to add a console input listener to an async app, and I've hit an interesting problem.</p>
  
  <p>The following code doesn't terminate when it should, and instead continues waiting for input after the script has terminated:</p>
  
  <pre><code>import asyncio
  
  
  loop = asyncio.get_event_loop()
  
  async def quit_after(timeout):
      await asyncio.sleep(timeout)
      print('main function terminating now') # this could simulate a crash, or a proper exit
  
  async def print_input_loop():
      while True:
          # executor thread is daemon - should terminate when script ends, but doesn't
          text = await loop.run_in_executor(None, input, '&gt; ')
          print(text)
  
  if __name__ == '__main__':
      loop.create_task(print_input_loop())
  
      try:
          loop.run_until_complete(quit_after(5))
      finally:
          loop.close()
  
      print('reached end of file - script should now terminate')
  </code></pre>
  
  <p>I've looked into other methods for getting input asynchronously, such as using <a href="https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.add_reader" rel="noreferrer">add_reader()</a> on <a href="https://docs.python.org/3.6/library/sys.html#sys.stdin" rel="noreferrer">sys.stdin</a>, but this doesn't work on Windows, and I need my solution to be platform independent.</p>
  
  <p>Does anyone know either a better way to get console input asynchronously that will gracefully halt when the app halts, or a way to fix my existing code so that it cleanly exits?</p>