API调用会引发错误,但会返回结果

时间:2020-10-20 21:27:12

标签: python api asynchronous python-requests jupyter-notebook

我有一个调用异步API的脚本。它会定期调用API,直到结果可用为止。我从Jupyter Notebook运行此脚本。

该脚本将输出正确保存在JSON文件中,但不会停止... 它还通过以下警告:

IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.

Current values:
NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
NotebookApp.rate_limit_window=3.0 (secs)

我知道可以使用一些其他命令来运行jupyter-notebook来禁用此警告。但是,即使我放下将结果保存到JSON的代码行(此警告消失了),该脚本也会进入循环并不会停止(我等待了1个小时)。

n_tries = 10
n_try = 0
wait_sec = 5
max_wait_sec = 60
while n_try < n_tries:
    try:
        resp = get(url = get_url, headers = {"Ocp-Apim-Subscription-Key": subscription_key})
        resp_json = resp.json()
        if resp.status_code != 200:
            print("GET results failed:\n%s" % json.dumps(resp_json))
            quit()
        status = resp_json["status"]
        if status == "succeeded":
            if output_path:
                with open(output_path, 'w') as outfile:
                    json.dump(resp_json, outfile, indent=2, sort_keys=True)
            print("Succeeded:\n%s" % json.dumps(resp_json, indent=2, sort_keys=True))
            quit()
        if status == "failed":
            print("Failed:\n%s" % json.dumps(resp_json))
            quit()   
        # Still running. Wait and retry.
        time.sleep(wait_sec)
        n_try += 1
        wait_sec = min(2*wait_sec, max_wait_sec) 
    except Exception as e:
        msg = "GET results failed:\n%s" % str(e)
        print(msg)
        quit()
    
print("The operation did not complete within the allocated time.")

任何想法为什么会发生?有什么办法可以解决?

P.S。该API可通过UI正常运行。仅当我在使用Python的Jupyter Notebook中调用它时,问题才会发生。

0 个答案:

没有答案