使用Google课堂API时出现随机死线超出错误

2024-09-28 13:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个运行在GAE(Python)上的应用程序,Google课堂用户可以在其中导入他/她的课程(学生的姓名和名单)。代码分为两部分。首先,我得到该用户的所有课程列表:

directoryauthdecorator = OAuth2Decorator(
approval_prompt='force',
client_id='my_client_id',
client_secret='my_client_secret',
callback_path='/oauth2callback',
scope=[
      'https://www.googleapis.com/auth/classroom.courses',
      'https://www.googleapis.com/auth/classroom.rosters'])

class ClassroomAPI(webapp.RequestHandler):
    @directoryauthdecorator.oauth_required
    def get(self):
        function=self.request.get('function')   
        auth_http = directoryauthdecorator.http()
        service = build("classroom", "v1", http=auth_http)

        if function == "getAllCourses":
            try:

                 results = service.courses().list(pageSize=100,teacherId=users.get_current_user().email(),courseStates="ACTIVE").execute()
                 courses = results.get('courses',[])

                 #PARSE AND RETURN LIST OF COURSES TO USER
            except errors.HttpError, error:
                 #RETURN ERROR


application = webapp.WSGIApplication(
                         [('/classroomAPI', ClassroomAPI),
                         (directoryauthdecorator.callback_path, directoryauthdecorator.callback_handler())],
                         debug=True)

这部分一直有效。然后用户从列表中选择要导入的课程。所选课程将发回上面的脚本,并执行下一部分:

        if function == "getStudentListForCourse":
            students=[]
            selectedCourses = json.loads(self.request.body)["courses"]
            for course in selectedCourses:
                page_token=None
                while True:
                    params={}
                    params["courseId"]=course["classroomId"]
                    params["pageSize"]=50
                    if page_token:
                        params["pageToken"]=page_token

                    studentList = service.courses().students().list(**params).execute()
                    for student in studentList['students']:
                        students.append(student['profile']['emailAddress'])

                    page_token = studentList.get('nextPageToken')
                    if not page_token:
                        break

            #RETURN STUDENTS

这里的问题是,我的日志在studentList = service.courses().students().list(**params).execute()行上随机报告'deadlineexceederror',这使得导入过程不可靠。你知道吗

任何提示都将不胜感激。你知道吗

更新:

我试过alpeware发布的建议,但不幸的是没有起到任何作用。你知道吗


Tags: clienttokenauthhttpgetifservicepage
1条回答
网友
1楼 · 发布于 2024-09-28 13:31:16

根据呼叫的持续时间,您将按对请求施加的60sec timeout。你不能改变这个超时,因为它可以帮助应用程序引擎完成它的伸缩魔术。你知道吗

为了避免超时,我建议使用Push Queue作为Task Queue服务的一部分。你知道吗

您必须重构代码,以适应UI中学生列表的异步加载,并将学生列表存储在数据存储中。你知道吗

在您的特定用例中,您可以重构代码以使用deferred library填充学生列表。你知道吗

要启用延迟库,您必须对app.yaml进行以下更改-

将以下条目添加到builtins部分的app.yaml

- deferred: on

以及同一文件的handlers部分的以下条目:

- url: /_ah/queue/deferred
    script: google.appengine.ext.deferred.deferred.application
    login: admin

下面是一些让你开始重构代码以使用延迟库的东西-

    from util import getStudentList

    if function == "getStudentListForCourse":
        credentials = directoryauthdecorator.get_credentials()
        selectedCourses = json.loads(self.request.body)["courses"]
        for course in selectedCourses:
            deferred.defer(getStudentList, course, credentials, None, [])

然后创建一个新模块util.py

     import httplib2
     from google.appengine.api import memcache

     def getStudentList(course=None, credentials=None, pageToken=None, students=None):
        http = httplib2.Http(cache=memcache)
        auth_http = credentials.authorize(http)
        service = build("classroom", "v1", http=auth_http)

        params = {}
        params["courseId"] = course["classroomId"]
        params["pageSize"] = 50
        if pageToken:
            params["pageToken"] = pageToken
        studentList = service.courses().students().list(**params).execute()
        for student in studentList['students']:
            students.append(student['profile']['emailAddress'])

        pageToken = studentList.get('nextPageToken')
        if pageToken:
            return deferred.deferr(getStudentList, course, credentials, pageToken, students)
        # There are no more students for this class.
        # TODO: store students for course

如前所述,当所有任务完成后,您必须更改视图以从数据存储返回结果。你知道吗

让我知道,如果你有进一步的问题,并乐意提供额外的指针。你知道吗

相关问题 更多 >

    热门问题