[英]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.