在Python中,是否可以针对批量HTTP请求的每个请求使用“指数退避”?

问题描述 投票:1回答:1

所以,在这里我写了一个脚本,将学生添加到课程中(Google Classroom 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)

它确实有效,但有时请求返回:

“请求https://classroom.googleapis.com/v1/courses/[COURSE ID时的HttpError 500”/学生?alt = json返回“内部错误”“

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

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

python google-api google-api-python-client exponential-backoff
1个回答
1
投票

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

回调有三个参数:

  • request_id:您决定识别要添加到批处理中的请求的ID(在调用批处理的add()方法时传递它
  • 响应:您对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保存所有错误请求,然后在第二时刻再次重试。有不同的方法可以执行此操作:您可以使用一个简单的列表并在运行批处理后进行检查,或者您可以创建一个专用的类并提前它提供的持久性。

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

© www.soinside.com 2019 - 2024. All rights reserved.