title: How to gracefully end asyncio program with CTRL-C when using loop run_in_executor
body: <p>The following code requires 3 presses of CTRL-C to end, how can I make it end with one only? (So it works nicely in Docker)</p>
  <pre class="lang-py prettyprint-override"><code>import asyncio
  import time
  
  
  def sleep_blocking():
      print(&quot;Sleep blocking&quot;)
      time.sleep(1000)
  
  
  async def main():
      loop = asyncio.get_event_loop()
      await loop.run_in_executor(None, sleep_blocking)
  
  
  try:
      asyncio.run(main())
  except KeyboardInterrupt:
      print(&quot;Nicely shutting down ...&quot;)
  </code></pre>
  <p>I've read many asyncio related questions and answers but can't figure this one out yet. The 1st CTRL-C does nothing, the 2nd prints &quot;Nicely shutting down ...&quot; and then hangs. The 3rd CTRL-C prints an ugly error.</p>
  <p>I'm on Python 3.9.10 and Linux.</p>
  <p>(edit: updated code per comment @mkrieger1)</p>