【发布时间】:2019-04-24 14:27:49
【问题描述】:
我有以下函数来运行 BigQuery 数据提取(见下文)。当我发送太多请求时,我收到错误:
google.api_core.exceptions.Forbidden: 403 Exceeded rate limits: too 此 project_and_region 的许多并发查询。更多 信息,见 https://cloud.google.com/bigquery/troubleshooting-errors
我想知道为什么我的代码无法捕捉到 Forbidden 错误,因为我明确编写了捕捉 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)
【问题讨论】:
-
回溯是否告诉你行号?我很好奇它是来自您的
try块内部还是来自您调用client.query -
它来自这条线:df = query_job.to_dataframe() 在try中
标签: python error-handling google-bigquery