簡體   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