【发布时间】:2017-12-17 01:30:07
【问题描述】:
当尝试在 Python 中的 Google API 上同时执行多项操作时,对于某些函数,我会收到“内部错误 - 500”。有时,当我运行 same 代码时,错误会出现在不同的行上。
这是我正在使用的方法的代码:
batch1 = service.new_batch_http_request(callback=callback)
batch2 = service.new_batch_http_request(callback=callback)
batch3 = service.new_batch_http_request(callback=callback)
batch1.add(service.courses().create(body=arCourse))
batch1.add(service.courses().create(body=ciCourse))
batch1.add(service.courses().create(body=dtCourse))
batch1.add(service.courses().create(body=drCourse))
batch1.add(service.courses().create(body=enCourse))
batch1.execute(http=http)
time.sleep(1)
batch2.add(service.courses().create(body=geCourse))
batch2.add(service.courses().create(body=hiCourse))
batch2.add(service.courses().create(body=icCourse))
batch2.add(service.courses().create(body=laCourse))
batch2.add(service.courses().create(body=maCourse))
batch2.execute(http=http)
time.sleep(1)
batch3.add(service.courses().create(body=muCourse))
batch3.add(service.courses().create(body=peCourse))
batch3.add(service.courses().create(body=reCourse))
batch3.add(service.courses().create(body=scCourse))
batch3.execute(http=http)
我已将其分成多个批次,并在批次之间包含一个“睡眠”,以避免超过 Google API 上每个用户每秒 5 次查询的限制,但我仍然遇到错误。
我在网上发现的一个类似问题是使用“指数退避”,但我不确定如何在这段代码中实现它们。
如果需要,我可以发布更大的代码示例。
【问题讨论】:
-
假设每个请求都将单独失败,并对失败的请求实施指数退避。有时,由于某些问题,Google 只会返回 500,请重试您的请求。
标签: python google-api google-classroom