繁体   English   中英

为什么我会收到配额限制错误? 谷歌云平台计算引擎虚拟机

[英]Why am I receiving a quota limit error? Google Cloud Platform Compute Engine VM

所以我在谷歌云计算引擎中设置了一个 python 脚本,该脚本设置为在一天中定期使用 cron 选项卡运行。 但是最近,脚本返回了这个错误,

gspread.exceptions.APIError: {'code': 429, 'message': "配额指标'读取请求'超出配额并限制消费者'project_number'服务'sheets.googleapis.com'的'每用户每分钟读取请求' :583704109550'.", 'status': 'RESOURCE_EXHAUSTED', 'details': [{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'RATE_LIMIT_EXCEEDED', 'domain' : 'googleapis.com', 'metadata': {'quota_metric': 'sheets.googleapis.com/read_requests', 'quota_limit': 'ReadRequestsPerMinutePerUser', 'service': 'sheets.googleapis.com', 'consumer': '项目/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)

Google 表格 API 对每 100 秒可以执行的请求数有限制。 这来自表格 API v4 的文档页面。

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

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

如果您的项目还没有结算帐号,请创建一个。 访问 API 控制台中 API 库的 Enabled APIs 页面,以及 select 列表中的 ZDB974238714CA8DE634ACE。 要查看和更改配额相关设置,请参阅 select Quotas。 要查看使用情况统计信息,请参阅 select 使用情况。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM