使用Google Classroom API时出现Random DeadlineExceeded错误

时间:2017-05-10 07:31:24

标签: python google-app-engine google-classroom

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

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

这里的问题是我的日志不断报告' DeadlineExceededError'在studentList = service.courses().students().list(**params).execute()行的随机时间,这使得导入过程不可靠。

任何提示都将不胜感激。

更新:

我已经尝试过alpeware发布的建议,但遗憾的是它并没有什么区别。

1 个答案:

答案 0 :(得分:0)

根据您的通话时间长短,您点击60sec timeout强制要求。您无法更改此超时,因为它可以帮助App Engine实现扩展魔术。

要解决超时问题,我建议您使用Push Queue作为Task Queue服务的一部分。

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

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

要启用延期库,您必须对app.yaml -

进行以下更改

将以下条目添加到builtins的<{1}}部分:

app.yaml

以及同一文件的处理程序部分的以下条目:

- deferred: on

这里有一些东西让你开始重构代码以使用延迟库 -

- 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

如上所述,您必须更改视图,以便在完成所有任务后从数据存储区返回结果。

如果您有其他问题,请告诉我,并乐意提供更多指示。