У меня есть следующая функция для запуска извлечения данных BigQuery (см. Ниже). Когда я отправляю слишком много запросов, я получаю сообщение об ошибке:
google.api_core.exceptions.Forbidden: 403 Превышено ограничение скорости: слишком
много одновременных запросов для этого project_and_region. Для большего
информация, см.
https://cloud.google.com/bigquery/troubleshooting-errors
Мне интересно, почему мой код не может перехватить запрещенную ошибку, поскольку я явно написал функцию для перехвата 403?
from google.cloud import bigquery
from google.api_core.exceptions import Forbidden, InternalServerError, ServiceUnavailable
def run_job(query, query_params, attempt_nb=1):
# Configure
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(
query,
# Location must match that of the dataset(s) referenced in the query.
location='US',
job_config=job_config) # API request - starts the query
# Try to run and transform to DataFrame()
try:
df = query_job.to_dataframe()
assert query_job.state == 'DONE'
return df
except Forbidden:
# Exception mapping a ``403 Forbidden`` response."""
return retry_job(query, query_params, attempt_nb)
except InternalServerError:
# Exception mapping a ``500 Internal Server Error`` response. or a :attr:`grpc.StatusCode.INTERNAL` error."""
return retry_job(query, query_params, attempt_nb)
except ServiceUnavailable:
# Exception mapping a ``503 Service Unavailable`` response or a :attr:`grpc.StatusCode.UNAVAILABLE` error."""
return retry_job(query, query_params, attempt_nb)
def retry_job(query, query_params, attempt_nb):
# If the error is a rate limit or connection error, wait and
# try again.
# 403: Forbidden: Both access denied and rate limits.
# 408: Timeout
# 500: Internal Service Error
# 503: Service Unavailable
# Old way: if err.resp.status in [403, 408, 500, 503]:
if attempt_nb < 3:
print(' ! New BigQuery error. Retrying in 10s')
time.sleep(10)
return run_job(query, query_params, attempt_nb + 1)
else:
raise Exception('BigQuery error. Failed 3 times', query)