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()
相关推荐
龙腾AI白云19 小时前
基于Transformer的人工智能模型搭建与fine-tuning
人工智能·数据挖掘
Aloudata1 天前
告别 90% 误报率:基于算子级血缘实现精准数据治理与变更影响分析
数据挖掘·数据治理·元数据·数据血缘
Faker66363aaa1 天前
基于YOLO11-Seg-EfficientViT的书籍缺陷检测与分类系统详解
人工智能·分类·数据挖掘
蚁巡信息巡查系统1 天前
网站信息发布再巡查机制怎么建立?
大数据·人工智能·数据挖掘·内容运营
2501_941337061 天前
蓝莓成熟度自动检测与分类_基于YOLO11-C3k2-AdditiveBlock-CGLU的深度学习实现
深度学习·分类·数据挖掘
Lun3866buzha1 天前
涡轮叶片表面缺陷识别与分类使用YOLOv8与特征金字塔共享卷积详解及代码实现
yolo·分类·数据挖掘
OLOLOadsd1231 天前
改进YOLO11-EMBSFPN-SC用于矿石矿物识别分类原创
人工智能·分类·数据挖掘
高洁011 天前
基于Transformer的人工智能模型搭建与fine-tuning
人工智能·算法·机器学习·数据挖掘·知识图谱
应用市场1 天前
基于稠密对应关系的3D人体网格回归技术详解
3d·数据挖掘·回归
ASD123asfadxv1 天前
YOLO13-seg-iRMB:陶瓷缺陷智能识别与分类系统实战_1
人工智能·分类·数据挖掘