在 Python 中轮询结果的惯用方法

问题描述

我需要编写一些代码来轮询 API 并根据响应中断,例如:

attempts = 0
poll_interval = 30
while attempts < 30:  # 15 minutes
    resp = requests.get(report_endpoint)
    resp.raise_for_status()
    status = resp.json['status']
    if status == 'PROCESSING':
        time.sleep(poll_interval)
    elif status == 'SUCCESS':
        # Do something
    attempts += 1

关于 while 循环 + 睡眠命令的一些东西只是感觉低于标准和草率。在 Python 中是否有更惯用的方法来做到这一点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)