WorldQuant--发送post请求时no location的处理办法

当你准备了一堆alpha 通过循环进行simulate的时候,由于登录过期等问题可能会出现如下报错

处理办法就是从中断的地方重新登录,重新开始,下面是一个解决问题的示例代码:

python 复制代码
from time import sleep
import json
import requests
from os.path import expanduser


def login():
    # Load credentials
    with open(expanduser('brain_credentials.txt')) as f:
        credentials = json.load(f)
    # Extract username and password from the list
    username, password = credentials
    # Create a session object
    sess = requests.Session()
    # Set up basic authentication
    sess.auth = HTTPBasicAuth(username, password)
    # Send a POST request to the API for authentication
    response = sess.post('https://api.worldquantbrain.com/authentication')
    # Print response status and content for debugging
    print(response.status_code)
    print(response.text)
    return sess


def main():
    alpha_list = []  # 这里应该是你的 alpha 列表,你需要根据实际情况填充
    sess = login()
    index = 0  # 记录当前处理的 alpha 的索引
    while index < len(alpha_list):
        alpha = alpha_list[index]
        try:
            sim_resp = sess.post(
                "https://api.worldquantbrain.com/simulations",
                json=alpha,
            )
            sim_progress_url = sim_resp.headers.get('Location')
            if sim_progress_url:
                while True:
                    sim_progress_resp = sess.get(sim_progress_url)
                    retry_after_sec = float(sim_progress_resp.headers.get("Retry-After", 0))
                    if retry_after_sec == 0:  # simulation done!
                        break
                    sleep(retry_after_sec)
                alpha_id = sim_progress_resp.json()["alpha"]  # the final simulation result
                print(alpha_id)
            else:
                print("no location, sleep for 10 seconds and try next alpha")
                sleep(10)
        except Exception as e:
            print(f"An error occurred: {e}")
            print("Re-login and try again from the next alpha.")
            sess = login()
            index += 1  # 从下一个 alpha 开始
        index += 1


if __name__ == "__main__":
    main()
相关推荐
比奇堡裤头村几秒前
统计学习方法——逻辑斯谛回归与最大熵模型
数据挖掘·回归·学习方法
正在走向自律21 分钟前
时序大模型 TimechoAI 实战指南,让时序数据分析从“手工建模“走向“智能预测“
数据挖掘·数据分析·timechodb·时序数据·时序大模型·timechoai
躺柒1 小时前
读数据可视化05视觉感知
信息可视化·数据挖掘·数据分析·视觉·视觉感知
leoZ2312 小时前
Vue3 还原一个企业级后台-14-项目总结
开发语言·人工智能·后端·opencv·计算机视觉·数据挖掘·rust
CCC:CarCrazeCurator18 小时前
DeepSeek‑Harness Windows 本地快速上手
深度学习·数据挖掘
databook18 小时前
如何对稀疏数据的场景进行分析
python·数据挖掘·数据分析
科技每日热闻1 天前
AI 赋能财务资金管理、风险预警、经营数据分析,合适的云上财务智能 Agent 有哪些?—— 优先采用 Amazon Quick 四链路解决方案
人工智能·ai·数据挖掘·数据分析
147API1 天前
蒸馏模型数据漂移怎么监控,从输入变化到回归验证
人工智能·数据挖掘·回归
leoZ2311 天前
Vue3 还原一个企业级后台-11-Mock数据策略
opencv·计算机视觉·数据挖掘·语音识别·caffe
躺柒2 天前
读数据可视化03可视化分类
信息可视化·分类·数据挖掘·数据可视化·数据科学·可视化分析·科学可视化