在此异步设置中,我在哪里捕获 KeyboardInterrupt 异常

Where do I catch the KeyboardInterrupt exception in this async setup

我正在开发一个使用 ccxt 异步库的项目,该库需要通过显式调用 class 来释放某个 class 使用的所有资源.close() 协程。我想用 ctrl+c 退出程序并等待异常中的关闭协程。然而,它从未被等待。

该应用程序由模块 harvestersstrategiestradersbrokermain(加上配置等)组成。经纪人启动为交易所指定的策略并执行它们。该策略启动收集必要数据的相关收割机。它还会分析数据并在有盈利机会时催生交易者。主模块为每个交易所创建一个代理并运行它。我试图在每个级别捕获异常,但从未等待关闭例程。我更愿意在主模块中捕获它以关闭所有交换实例。

收割机

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)

策略

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)

经纪人

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)

主要

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)

我原以为 close() 协程会被等待,但我仍然从库中得到一个错误,我必须明确地调用它。我在哪里捕获异常以便正确关闭所有交换实例?

代码中的某处应该是入口点,事件循环开始的地方。

通常是以下函数之一:

loop.run_until_complete(main())

loop.run_forever()

asyncio.run(main())

ctrl+C发生时KeyboardInterrupt可以在这一行被捕获。当它碰巧执行一些终结协程时,你可以再次 运行 事件循环。

这个小例子展示了想法:

import asyncio 


async def main():
    print('Started, press ctrl+C')
    await asyncio.sleep(10)


async def close():
    print('Finalazing...')
    await asyncio.sleep(1)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    except KeyboardInterrupt:
        loop.run_until_complete(close())
    finally:
        print('Program finished')