title: Where do I catch the KeyboardInterrupt exception in this async setup
body: <p>I am working on a project that uses the <code>ccxt</code> async library which requires all resources used by a certain class to be released with an explicit call to the class's <code>.close()</code> coroutine. I want to exit the program with <code>ctrl+c</code> and await the close coroutine in the exception. However, it is never awaited.</p>
  
  <p>The application consists of the modules <code>harvesters</code>, <code>strategies</code>, <code>traders</code>, <code>broker</code>, and <code>main</code> (plus config and such). The broker initiates the strategies specified for an exchange and executes them. The strategy initiates the associated harvester which collects the necessary data. It also analyses the data and spawns a trader when there is a profitable opportunity. The main module creates a broker for each exchange and runs it. I have tried to catch the exception at each of these levels, but the close routine is never awaited. I'd prefer to catch it in the main module in order to close all exchange instances.</p>
  
  <p>Harvester</p>
  
  <pre class="lang-py prettyprint-override"><code>async def harvest(self):
      if not self.routes:
          self.routes = await self.get_routes()
      for route in self.routes:
          self.logger.info("Harvesting route {}".format(route))
          await asyncio.sleep(self.exchange.rateLimit / 1000)
          yield await self.harvest_route(route)
  </code></pre>
  
  <p>Strategy</p>
  
  <pre class="lang-py prettyprint-override"><code>async def execute(self):
      async for route_dct in self.harvester.harvest():
          self.logger.debug("Route dictionary: {}".format(route_dct))
          await self.try_route(route_dct)
  </code></pre>
  
  <p>Broker</p>
  
  <pre class="lang-py prettyprint-override"><code>async def run(self):
      for strategy in self.strategies:
          self.strategies[strategy] = getattr(
              strategies, strategy)(self.share, self.exchange, self.currency)
      while True:
          try:
              await self.execute_strategies()
          except KeyboardInterrupt:
              await safe_exit(self.exchange)
  </code></pre>
  
  <p>Main</p>
  
  <pre class="lang-py prettyprint-override"><code>async def main():
      await load_exchanges()
      await load_markets()
      brokers = [Broker(
          share,
          exchanges[id]["api"],
          currency,
          exchanges[id]["strategies"]
          ) for id in exchanges]
      futures = [broker.run() for broker in brokers]
      for future in asyncio.as_completed(futures):
          executed = await future
          return executed
  
  
  if __name__ == "__main__":
      status = asyncio.run(main())
      sys.exit(status)
  </code></pre>
  
  <p>I had expected the <code>close()</code> coroutine to be awaited, but I still get an error from the library that I must explicitly call it. Where do I catch the exception so that all exchange instances are closed properly?</p>