I've been using Google Vision API to perform OCR tasks in some documents using Python.
It begins working perfectly, until I start receiving Http Error Code 429, which means I am doing too many requests in a short amount of time. Then, I decided to put a sleep between each request, of which time increases as the number of Http Error Code 429 increases. However, after some time, the error message keeps coming. Since the messages keeps arriving, the sleeping time keeps increasing until it reaches a point that it sleeps for so long that I lose connection.
The weirdest thing is that if I receive such error message many times in a row and, immediately, finish the process and start it again, the requests start to work again in the first try.
In other words, it seems that no matter the sleeping time I put I will start receiving such messages at some point and the only way to put it work again is restarting the process (which makes no sens at all).
How can I avoid having such error message without having to restart the process? Can anyone help me?
Thanks a lot!
EDIT:
This is the code of the request (part of it).
from apiclient import discovery
from oauth2client.client import GoogleCredentials
# The other imports are omitted
DISCOVERY_URL = 'https://{api}.googleapis.com/$discovery/rest?version={apiVersion}' # noqa
credentials = GoogleCredentials.get_application_default()
self.vision = discovery.build(
'vision', 'v1', credentials=credentials,
discoveryServiceUrl=DISCOVERY_URL)
batch_request = []
for image in images:
batch_request.append({
'image': {
'content': base64.b64encode(image).decode('UTF-8')
},
'features': [{
'type': 'TEXT_DETECTION',
}]
})
request = self.vision.images().annotate(
body={'requests': batch_request})