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

iuv*_*bio 5 python asynchronous python-asyncio ccxt

我正在开发一个使用ccxt异步库的项目,该项需要释放某个类所使用的所有资源,并显式调用该类的.close()协同程序.我想退出程序ctrl+c并等待异常中的关闭协程.但是,它永远不会被等待.

该应用程序包含的模块harvesters,strategies,traders,broker,和main(加上配置和这样).经纪人启动为交易所指定的策略并执行它们.该策略启动收集必要数据的相关收集器.它还分析数据并在有利可图的机会时产生交易者.主模块为每个交换创建一个代理并运行它.我试图在每个级别捕获异常,但是从未等待过关闭例程.我宁愿在主模块中捕获它以关闭所有交换实例.

收割机

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)
Run Code Online (Sandbox Code Playgroud)

战略

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)
Run Code Online (Sandbox Code Playgroud)

经纪人

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)
Run Code Online (Sandbox Code Playgroud)

主要

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)
Run Code Online (Sandbox Code Playgroud)

我曾close()期待等待协程,但我仍然从库中得到一个我必须明确称之为错误的错误.我在哪里捕获异常以便正确关闭所有交换实例?

Mik*_*mov 5

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

通常它是以下功能之一:

loop.run_until_complete(main())

loop.run_forever()

asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)

什么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')
Run Code Online (Sandbox Code Playgroud)

  • 我想没有办法从异步循环中运行的任何任务中捕获“KeyboardInterrupt”,以“本地”处理它?因为无法确保“KeyboardInterrupt”将传递给该任务而不是另一个任务(或某些内部事件循环代码)? (2认同)