I’m trying to fetch multiple emails quickly using the Gmail API with the following function:
def get_mails(self, ids):
"""
Retrieve multiple emails using batch requests.
It's recommended to use a maximum of 20 in a single call.
"""
batch = self.service.new_batch_http_request()
mails_map = {}
def callback(request_id, response, exception):
if exception is not None:
print(f"Error occurred for {request_id}: {exception}")
else:
mail_id = response['id']
mails_map[mail_id] = response
for idx, msg_id in enumerate(ids):
batch.add(self.service.users().messages().get(userId="me", id=msg_id), request_id=str(idx), callback=callback)
batch.execute()
return mails_map
Currently, it takes about 20 minutes to fetch 10,000 emails, which is quite slow. I can’t fetch more than 20 emails at a time using batch requests, as it triggers rate limit errors. However, I can’t find any specific rate limit values mentioned in the Gmail API documentation for batch requests.
Is there a way to increase the number of emails fetched in a single batch request without hitting the rate limits? Or is there a better approach to optimize this process and speed things up? Any guidance would be greatly appreciated. Thanks!
7