已解决1.57K 浏览
0

使用了多个if api.wait_update(deadline=60):老是抛出如下异常

raise Exception(“不能在协程中调用 wait_update, 如需在协程中等待业务数据更新请使用 register_update_notify”)
Exception: 不能在协程中调用 wait_update, 如需在协程中等待业务数据更新请使用 register_update_notify

在有数据接收时会正常,没数据接收时包括网络延时都会弹出异常,文档里看了register_update_notify,但不会使用,请问怎么处理呢?

owenlovehellen 选择最佳答案 2022年3月4日
0

不要使用多个api.wait_update,在主线程中保持一个api.wait_update更新数据即可,如果想多品种同时监控,可以协程里使用register_update_notify

1.子进程中开启一份api.wait_update,它负责更新所有数据

2.定义方法,动态添加协程任务

3.定义方法,要实现的逻辑写这里

希望能帮助到你

def 保持更新的方法(self):
    tqNotify = TqNotify(SI.api)  # 构造实例类
    while True:
        self.api.wait_update()
        SI.notify_list = tqNotify.get_notifies()
        for notify in SI.notify_list:
            print(f'打印出通知内容--{notify}')  # 打印出通知内容

def 动态添加协程(self, lists):
    print(f'add_creat--收到添加信息{lists}')
    for sy in lists:
        # 获取大周期的K线
        self.api.create_task(self.demo3(sy, name=f'{sy}_day'))
        # 获取小周期的K线
        self.api.create_task(self.demo3(sy, name=f'{sy}_min', duration_seconds=60, data_length=8000))

async def demo3(self, SYMBOL, name, duration_seconds=60 * 60 * 24, data_length=120):

        # 使用 await 框架自带循环等待数据,不需要再次循环等待取K线
        klines = await api.get_kline_serial(SYMBOL, duration_seconds, data_length)

        # 具体自己要实现的逻辑代码

        # 执行完后退出
        print(f'{name}--历史数据{len(klines)}已准备好,退出...')
        return True
owenlovehellen 发表新评论 2022年3月4日

好家伙,还是没看明白,我直接try exception抛出异常,因为有数据接收能正常运行

您正在查看1个答案中的1个,单击此处查看所有答案。