【问题标题】:Why am I receiving a quota limit error? Google Cloud Platform Compute Engine VM为什么我会收到配额限制错误?谷歌云平台计算引擎虚拟机
【发布时间】:2021-03-17 02:50:10
【问题描述】:

因此,我在 Google Cloud Compute 引擎中设置了一个 python 脚本,该脚本设置为在一天中定期使用 cron 选项卡运行。但是最近,脚本返回这个错误,

gspread.exceptions.APIError: {'code': 429, 'message': "配额指标“读取请求”超出配额,并限制服务“sheets.googleapis”的“每用户每分钟读取请求”。 com' for consumer 'project_number:583704109550'.", 'status': 'RESOURCE_EXHAUSTED', 'details': [{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': ' RATE_LIMIT_EXCEEDED','域':'googleapis.com','元数据':{'quota_metric':'sheets.googleapis.com/read_requests','quota_limit':'ReadRequestsPerMinutePerUser','服务':'sheets.googleapis.com ', '消费者': 'projects/583704109550'}}]}

我试图研究为什么它会抛出它,但由于我在该领域缺乏经验而不知所措。这是任何人都想知道的脚本,

from woocommerce import API
from df2gspread import df2gspread as d2g
from df2gspread import gspread2df as g2d
from collections import defaultdict
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def parseData(entry):
        #Extract relectant information (all key value pairs that are listed under meta_data)
        d = {pair["key"]:pair["value"] for pair in entry["line_items"][0]["meta_data"]}
        d["Event"] = entry["line_items"][0]["name"]
        return d
wcapi = API(
    url="REDACTED",
    consumer_key="REDACTED",
    consumer_secret="REDACTED",
    version="wc/v3",
    query_string_auth="true"
)
entries = []
pageNum = 1
while True:
        #API is paginated with products per page limit of 100
        rawEntries = wcapi.get("orders", params = {"per_page": 100, "page": pageNum}).json()
        #Page until there are no entries on a page
        if len(rawEntries) == 0:
                break
        entries.extend([parseData(e) for e in rawEntries])
        pageNum += 1
rawEvents = defaultdict(list)
for entry in entries:
        #Organize entries by their event
        rawEvents[entry["Event"]].append(entry)
events = {k: pd.DataFrame(v).fillna('') for (k,v) in rawEvents.items()} #Built a dataframe for each event
#Upload to Google Sheets using gspread and df2gspread
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/home/shahav2016/GoogleKeyJSON.json', scope)
gc = gspread.authorize(credentials)
spreadsheet_key = 'REDACTED FOR PRIVACY REASONS' #The name of the spreadsheet - look at the URL
for event, df in events.items():
        #Filter out test events
        if event not in ['REDACTED FOR PRIVACY']:
                #Upload the data to the correct sheet
                d2g.upload(df, spreadsheet_key, event, row_names = False, credentials = credentials)

【问题讨论】:

  • 您的谷歌云帐户对您每分钟可以执行的读取次数有限制,而您已超过该限制。

标签: python google-sheets google-cloud-platform


【解决方案1】:

Google Sheets API 对每 100 秒可以处理的请求数有限制。这是来自 Sheets API v4 的 documentation 页面。

此版本的 Google Sheets API 的每个请求限制为 500 个 每个项目 100 秒,每个用户每 100 秒 100 个请求。 单独跟踪读取和写入的限制。没有日常 使用限制。

查看或更改项目的使用限制,或请求 增加您的配额,请执行以下操作:

如果您还没有项目的结算帐号,那么 创建一个。访问 API 中 API 库的 Enabled APIs 页面 控制台,然后从列表中选择一个 API。查看和更改 配额相关设置,选择配额。要查看使用统计信息, 选择用法。

此外,有三个选项可以解决此限制:

  1. 增加您在开发者控制台中调用的 Sheets API 的quota limit
  2. 由于您使用的是 for 循环,因此请求的发送速度非常快。我认为在该循环中的某处放置 sleep 是明智的,这样您就不会超过每 100 秒的 100 个请求。
  3. 如果事情进展得太慢,请尝试一次将多个更改上传到电子表格,这种机制称为batching。这也将减少 API 请求的数量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多