BatchHttpRequest:如何从批处理中的所有请求收集结果?

2024-04-27 22:47:40 发布

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

我正在使用BatchHttpRequest(https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch)从多个Http请求收集结果:

for user in all_users_in_domain:
    gmail_service = build_gmail_service_for_user(user.google_user_id)
    batch.add(gmail_service.service.users().messages().list(userId='me', q=search_query), callback=get_email_list, request_id=user_id)
batch.execute()

然后我想在回调函数get_email_list中使用我自己的逻辑来处理这些聚合结果

^{pr2}$

如何收集所有回调中的数组消息\u列表,以便对请求批中返回的所有结果运行我的算法?在


Tags: inhttpsidcloudforgetemailservice
1条回答
网友
1楼 · 发布于 2024-04-27 22:47:40

有几种方法可以做到这一点,比如在类中包含代码并附加到类属性(例如。self.message_列表). 在

也可以将回调函数放在另一个函数中:

def some_outer_function():
  some_outer_function.message_list = ''

  def get_email_list(request_id, response, exception):
      some_outer_function.message_list += response['messages']

  for user in all_users_in_domain:
      gmail_service = build_gmail_service_for_user(user.google_user_id)
      batch.add(gmail_service.service.users().messages().list(userId='me', q=search_query), callback=get_email_list, request_id=user_id)
  batch.execute()

请注意,您必须用外部函数名限定“message_list”。如果您只需参考“邮件列表”,您将分配给一个变量,该变量是“获取电子邮件列表”的本地变量。在

你也可以只使用一个全局模块变量,但我不太喜欢。类方法是我最喜欢的选择。在

相关问题 更多 >