【问题标题】:python threading, confirming responses before moving to next linepython线程,在移动到下一行之前确认响应
【发布时间】:2020-09-27 12:11:57
【问题描述】:

最近我一直在努力整合谷歌目录、日历和课堂,以便与我们现有的服务无缝协作。 我需要遍历 1500 个对象并在 google 中发出请求以检查某些内容。来自 google 的响应确实需要一段时间,因此我想等待该请求完成,但同时运行其他检查。

    def __get_students_of_course(self, course_id, index_in_course_list, page=None):
        print("getting students from gclass ", course_id, "page ", page)
        # self.__check_request_count(10)
        try:
            response = self.class_service.courses().students().list(courseId=course_id,
                                                                    pageToken=page).execute()
            # the response must come back before proceeding to the next checks
            course_to_add_to = self.course_list_gsuite[index_in_course_list]
            current_students = course_to_add_to["students"]
            for student in response["students"]:
                current_students.append(student["profile"]["emailAddress"])
                self.course_list_gsuite[index_in_course_list] = course_to_add_to
            try:
                if "nextPageToken" in response:
                    self.__get_students_of_course(
                        course_id, index_in_course_list, page=response["nextPageToken"])
                else:
                    return
            except Exception as e:
                print(e)
                return
        except Exception as e:
            print((e))

我从另一个函数运行该函数

    def __check_course_state(self, course):
        course_to_create = {...}
        try:
            g_course = next(
                (g_course for g_course in self.course_list_gsuite if g_course["name"] == course_to_create["name"]), None)

            if g_course != None:
                index_2 = None
                for index_1, class_name in enumerate(self.course_list_gsuite):
                    if class_name["name"] == course_to_create["name"]:
                        index_2 = index_1

                self.__get_students_of_course(
                    g_course["id"], index_2) # need to wait here
                students_enrolled_in_g_class = self.course_list_gsuite[index_2]["students"]

                request = requests.post() # need to wait here
                students_in_iras = request.json()
                students_to_add_in_g_class = []
                for student in students["data"]:
                    try:
                        pass
                    except Exception as e:
                        print(e)
                        students_to_add_in_g_class.append(
                            student["studentId"])

                if len(students_to_add_in_g_class) != 0:
                  pass
                else:
                    pass
            else:
                pass
        except Exception as e:
            print(e)

我需要为 1500 个对象完成这些任务。 虽然它们之间没有任何关系。我想移动到循环中的下一个对象,同时等待其他结果返回并完成。 这是我用线程尝试的方法:

    def create_courses(self):
        # pool = []
        counter = 0
        with concurrent.futures.ThreadPoolExecutor() as excecutor:
            results = excecutor.map(
                self.__check_course_state, self.courses[0:5])

问题是当我这样运行它时,我得到了多个 SSL 错误和其他错误,据我所知,由于线程本身正在运行,请求永远不会等待完成并移动到下一行,因此我什么都没有在请求对象中,所以它会引发错误? 关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: python multithreading google-api io python-requests


    【解决方案1】:

    出现 ssl 错误是因为我正在重用来自 google api lib 的 http 实例。 self.class_service 用于在等待另一个请求时发送请求。处理此问题的最佳方法是在每个请求上创建 service 的实例。

    【讨论】:

      猜你喜欢
      • 2021-04-17
      • 1970-01-01
      • 2020-11-19
      • 2020-10-30
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      相关资源
      最近更新 更多