Gre*_*ech 2 python python-requests
我正在使用 Python 中的请求库进行 POST 调用。我的 POST 电话大约需要 5 分钟才能完成。它将在 S3 存储桶中创建一个文件。之后,我想下载这个文件。但是,我需要创建一个额外的逻辑来等待我的 POST 完成,然后再执行我的代码的下一行以下载文件。
有什么建议?
是否可以为此使用子进程库?如果是这样,语法将如何?
代码:
import requets
r = requests.post(url)
# wait for the post call to finish
download_file(file_name)
Run Code Online (Sandbox Code Playgroud)
问题说 POST 请求需要 5 分钟才能返回,但这也许不太正确?也许 POST 请求会立即返回,但服务器会继续花费 5 分钟为 S3 存储桶创建文件?在这种情况下,延迟的需要是有道理的。需要单独下载的事实往往支持这种解释(所请求的信息不会从请求本身返回)。
如果下载失败引发异常,请尝试以下操作:
import time
r = requests.post(url)
while True:
time.sleep(60) # sixty second delay
try:
download_file(file_name)
break
except Error:
print ("File not ready, trying again in one minute")
Run Code Online (Sandbox Code Playgroud)
或者如果 download_file 在失败时简单地返回 False:
import time
r = requests.post(url)
while True:
time.sleep(60) # sixty second delay
if download_file(file_name):
break
print ("File not ready, trying again in one minute")
Run Code Online (Sandbox Code Playgroud)
由于我对这个问题的解释是推测性的,如果这个答案不切题,我会删除它。
它应该已经等到它完成。
Python 与 Node.js 不同,默认情况下会阻止请求。如果您想异步运行它,则必须在另一个线程中显式运行它。如果您的 POST 请求需要 5 分钟才能获取,那么下载行将在 5 分钟结束并且 POST 请求完成之前不会运行。
| 归档时间: |
|
| 查看次数: |
19964 次 |
| 最近记录: |