我正在开发一个使用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 …Run Code Online (Sandbox Code Playgroud)