0

그래서 여기에 학생들을 코스 (Google 클래스 룸 API)에 추가하는 스크립트를 작성했습니다.파이썬에서는 일괄 처리 된 HTTP 요청에 대해 요청 당 '지수 백 오프'를 사용할 수 있습니까?

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) 

그것은 그러나, 때로는 개별 요청은 일 반환 않습니다

내부 오류 ""

을? "HttpError 500] https://classroom.googleapis.com/v1/courses/[COURSE ID를 요청할 때/학생들은 고도 = JSON 반환" 그리고이 개별 요청에 대해 5xx 오류가 발생하면 실패한 개별 요청을 다시 시도하도록 코드를 작성하고 싶습니다. 나는 이것을 어떻게 구현하는지 확신 할 수 없다.

현재 1 명의 학생 만 코스를 만들지 않았다면 전체 스크립트를 다시 실행해야합니다. 물론 리소스의 낭비입니다.

답변

0

일괄 처리를 만들 때 일괄 처리에 추가하는 각 요청에 대해 호출되는 콜백 함수를 제공 할 수 있습니다.을 REQUEST_ID

  • :

    콜백은 세 개의 매개 변수를 id를 당신이 배치에 추가하는 요청을 (당신이 배치의 추가() 메서드를 호출 할 때 당신이 그것을 통과 식별하기로 결정

  • 응답 : 당신이 API를
  • 예외에 만들어 놓은 단일 통화의 응답 : 배치의 요청에 오류가 Thew 보낸 경우 예외 객체

아래에는 논리를 설명하는 의사 코드가 있습니다.

# 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을 살펴 보시기 바랍니다.