我的代码如下,由于asyncio的原因,目前循环的内容是乱序执行的。我想等bars[1:]执行完后,再执行bars[0],我该如何处理?
或者,有没有办法在循环到bar[0]的时候,把协程等待几秒,先执行其他的bar?
为节约时间考虑,我的代码分钟级别,这里改成秒级
from tqsdk import TqApi, TqAuth import datetime api = TqApi(web_gui=True, auth=TqAuth()) def my_function(code, bar): df = d[code + '_' + str(bar)] if api.is_changing(df.iloc[-1], 'datetime'): dt = datetime.datetime.fromtimestamp(df["datetime"].to_numpy()[-1] / 1e9) print(code, bar, dt) if bar == bar[0]: print() pass codes = ['INE.sc2108'] bars = [5, 15, 30, 60, 60*5, 60*15, 60*30, 60*60] d = {} for code in codes: for bar in bars: d[code + '_' + str(bar)] = api.get_kline_serial(symbol=code, duration_seconds=bar, data_length=500) while True: api.wait_update() for code in codes: for bar in bars[::-1]: my_function(code=code, bar=bar)
即使我在bars循环里面逆序,也经常会出现bar[0]先更新的情况:
INE.sc2108 5 2021-07-05 11:04:00 INE.sc2108 60 2021-07-05 11:04:00 INE.sc2108 30 2021-07-05 11:04:00 INE.sc2108 15 2021-07-05 11:04:00