如何阻塞异步函数,让其全部完成后,主函数再return

import threading

def download_history_data2(stock_list, period, start_time='', end_time='', callback=None, incrementally=None):
client = get_client()

# 创建一个 Event 对象,用于阻塞直到 supply_history_data2 完成
event = threading.Event()

# 定义回调函数,当 download 完成时触发
def on_complete(result):
if callback:
callback(result)
# 设置事件,解除阻塞
event.set()

# 调用 supply_history_data2,并传入回调函数
client.supply_history_data2(stock_list, period, start_time, end_time, bson.BSON.encode(param), on_complete)

# 阻塞,直到事件被设置,表示下载完成
event.wait()

# 一旦事件被触发,说明下载已完成,返回结果
return '下载完成'

posted @ 2025-01-17 22:18  笨笨和呆呆  阅读(32)  评论(0)    收藏  举报