简体   繁体   English

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

[英]BatchHttpRequest: How can I collect the results from all requests in the batch?

I am using BatchHttpRequest ( https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch ) to collect results from multiple Http requests: 我正在使用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()

and then I'd like to process these aggregated results using my own logic inside the callback function get_email_list 然后我想在回调函数get_email_list使用自己的逻辑来处理这些汇总结果

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

How can I collect the array message_lists across all callbacks so I run my algorithm over all results returned in the batch of requests? 如何收集所有回调中的message_lists数组,以便对一批请求中返回的所有结果运行算法?

There are a few ways to do this, such as including your code in a class and appending to a class property (eg self.message_list). 有几种方法可以做到这一点,例如将代码包含在类中并追加到类属性(例如self.message_list)上。

Or you can place the callback function inside another function: 或者,您可以将回调函数放在另一个函数中:

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()

Note that you have to qualify "message_list" with the outer function name. 请注意,您必须使用外部函数名称来限定“ message_list”。 If you simply refer to "message_list" you will be assigning to a variable that is local to "get_email_list". 如果仅引用“ message_list”,则将分配给“ get_email_list”本地变量。

You can also just use a global module variable, but I'm not a big fan of that. 您也可以只使用全局模块变量,但是我并不赞成这样做。 The class method would be my favorite option. 类方法将是我最喜欢的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 请求模块从仅拨打搜索结果页面的前 10 个结果中抓取所有结果? - How can I scrape all the results not only first 10 results from just dial search result page using python requests module? 如何使用Python从Web API收集所有结果? - How to collect all results from a web API in Python? Pyparsing:如何从组中收集所有命名结果? - Pyparsing: How to collect all named results from groups? 如何从对象收集数据 - How can I collect data from objects 如何一次使用 SqlQuery 从数据库中收集所有数据? - How can i collect all the Data from a Database by using SqlQuery at once? 如何从多个输入中收集成对的数字,然后在最后一次打印它们? - How can I collect pairs of numbers from multiples inputs, then print them all at once at the end? 如何从 kubeflow 并行 for 循环收集所有输出? - How do I collect all outputs from a kubeflow parallel for loop? 如何将结果从注释收集到数组中? - How to collect results into array from annotation? 如何使用Selenium和Python从div中收集这些数据 - How can I collect this data from a div using Selenium and Python 如何使用 beautifulsoup + requests 缩小刮擦的结果? - How can I narrow down the results of a scrape with beautifulsoup + requests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM