在 Python 中,是否可以对每个请求使用 'exponential backoff' 来进行批处理 HTTP 请求?

In Python, is it possible to use 'exponential backoff' per request for batched HTTP requests?

所以,我在这里写了一个脚本,可以将学生添加到课程中(Google 教室 API)。

students = getStudents('Year10', '10A')  # VAR

for student in students:
    newStudent = {
        # Student Identifier
        'userId': student
    }
    batch1_1.add(service.courses().students().create(courseId=arCourseId, body=newStudent))
    batch1_1.add(service.courses().students().create(courseId=ciCourseId, body=newStudent))
    batch1_1.add(service.courses().students().create(courseId=dtCourseId, body=newStudent))
    batch1_1.add(service.courses().students().create(courseId=drCourseId, body=newStudent))
    batch1_1.add(service.courses().students().create(courseId=enCourseId, body=newStudent))
    batch1_2.add(service.courses().students().create(courseId=geCourseId, body=newStudent))
    batch1_2.add(service.courses().students().create(courseId=hiCourseId, body=newStudent))
    batch1_2.add(service.courses().students().create(courseId=icCourseId, body=newStudent))
    batch1_2.add(service.courses().students().create(courseId=laCourseId, body=newStudent))
    batch1_2.add(service.courses().students().create(courseId=maCourseId, body=newStudent))
    batch1_3.add(service.courses().students().create(courseId=muCourseId, body=newStudent))
    batch1_3.add(service.courses().students().create(courseId=peCourseId, body=newStudent))
    batch1_3.add(service.courses().students().create(courseId=reCourseId, body=newStudent))
    batch1_3.add(service.courses().students().create(courseId=scCourseId, body=newStudent))
batch1_1.execute()
time.sleep(1)
batch1_2.execute()
time.sleep(1)
batch1_3.execute()
time.sleep(1)

确实有效,但是,有时个别请求return:

"HttpError 500 when requesting https://classroom.googleapis.com/v1/courses/[COURSE ID]/students?alt=json returned "内部错误“”

对于这些单独的请求,我想编写代码以便在收到 5xx 错误时重试单独失败的请求。不过我不确定如何实现它。

目前,即使只有 1 名学生没有参加课程,我也不得不重新 运行 整个脚本,这当然是一种资源浪费。

创建批处理时,您可以提供一个回调函数,该函数将为您添加到批处理中的每个请求调用。

回调接受三个参数:

  • request_id:您决定标识要添加到批处理中的请求的 ID(在调用批量
  • 响应:您对 API 进行的单次调用的响应
  • exception: 批量请求出错时的异常对象

下面你有一些伪代码来解释逻辑。

# sample callback function
def my_batch_callback(request_id, response, exception):
    if exception is not None:
        # Do something with the exception
        print(exception)
    else:
        # Do something with the response
        print("Request is successful: {}".format(response))
    pass

# creation of batch passing in the call back
batch = service.new_batch_http_request(callback=my_batch_callback)

# addition to batch with a specific id
batch.add(service.object().insert(name="test-1", request_id="id-1"))
batch.add(service.object().insert(name="test-2", request_id="id-2"))
batch.add(service.object().insert(name="test-3", request_id="id-3"))

使用回调,您可以使用它们的 id 保存所有错误的请求,并在一秒钟内重试。有不同的方法可以做到这一点:您可以使用一个简单的列表并在 运行 批处理后检查它,或者您可以创建一个专用的 class 并利用它提供的持久性。

我建议你也看看官方文档here