【问题标题】:Python Google API resource limit errorPython Google API 资源限制错误
【发布时间】:2019-01-24 04:18:50
【问题描述】:

我了解资源限制,您可以等一下再去。 所以我做了一个运行直到达到限制的代码,然后等待 6 分钟,再次运行,等等。只是为了测试时间限制。

嗯,有时 6 分钟就足够了,有时则不够。我需要一种一致的方法来克服这个资源限制。

counter=0
try:
    for i in range(5000): # high enough to break limit
        client.open(google_sheet).sheet1
        counter+=1
        sleep(.02)
        print counter
except Exception as e:
    print "ERROR"
    if re.search('"code": 429', str(e)): #confirms error is due to resource limit
        sleep(360)
        for i in range(5000): 
            sheets_of_months[1].sheet_object
            counter+=1
            sleep(.02)
            print counter

我让它运行直到出现错误,等待 6 分钟,再次运行。打印只是为了避免任何无意义的等待。

感谢您的阅读和所有回复!

【问题讨论】:

    标签: python python-2.7 api google-api


    【解决方案1】:

    Google 建议的管理配额限制的方法是使用 exponential backoff logic 实现请求。我确实建议查看此文档(由 Google 提供):

    如果你看一下,你会发现不同的库可以帮助你进行指数退避:

    看看你的代码,使用坚韧你能做的是:

    import tenacity
    
    
    # Wait 2^x * 0.5 seconds between each retry, up to 60 seconds
    # 60 seconds afterwards.
    # Stops after 30 attempts
    @tenacity.retry(stop=tenacity.stop_after_attempt(30),
                    wait=tenacity.wait_exponential(multiplier=0.5, max=60))
    def call_with_exponential_backoof(client, google_sheet):
        client.open(google_sheet).sheet1
    

    【讨论】:

    • 遗憾的是,我已经阅读并实施了 Google 的指数回退,但它没有帮助,这是因为我的配额限制据说是每 100 秒。但是当我等待超过 6 分钟时,它有时仍然无法正常工作。我试过添加随机的毫秒数,遗憾的是没有区别。
    • 我尝试通过使用坚韧的示例来改进我的答案。
    【解决方案2】:

    好的,所以我在阅读并给出了几个小时来运行不同的代码之后想通了。所以this post 真的帮助了我,因为我还注意到 Google 的帖子不一致。但答案指出了我需要思考的问题。

    “关于算法的重要部分是它的指数,而不是尝试次数。”

    Google 的文档说要在 16 秒后停止并记录下来。

    所以我尝试了一些答案,

    1. 在尝试重新加载页面之前,我开始增加睡眠时间。甚至长达 20 分钟,有时它仍然无法正常工作。但有时 30 秒就足够了。

    2. 我尝试按照文档中的建议在睡眠量上添加随机秒数,以使其保持不同和随机,但我相信这更适用于网络,而不是一个程序和一张纸通信,没有其他预计会干扰的机器。

    3. 我尝试更改页面,可能我打开的页面已被锁定,因为我达到了它的资源限制。

    我基本上做了一个小的 while 循环,它一次又一次地运行,当它工作时,它会打印出资源限制冷却需要多长时间。我有时对这些变量中的每一个进行测试,并得出以下结论。

    您尝试打开的页面永远不会锁定,无需切换页面,虽然它似乎对我有用,但这只是运气。进一步的研究证明它没有用。

    有时它只会说你仍然处于限制状态,无论这是否是由于它以某种方式尝试在睡眠期间打开文件以进行准备,idk。我所知道的只是 20 分钟的睡眠,它仍然会说我处于极限。

    随机整数虽然可能对防止请求捆绑的网络问题有用,但对我来说不是问题。所以多余的代码,意味着摆脱它。

    我制作的这个循环平均大约需要 30 秒,有时甚至是一分钟。但做的工作。感谢所有阅读并关心查看帖子的人。我希望我能很好地解释我的思考过程,并以某种方式为社区提供帮助。

    times_page_opened=0
    amount_of_seconds_iterating= 2
    while True:
        counter=0 
        try:
            for i in range(5000):
                counter+=1
                if counter==2: # This confirms that it has opened a page again at least once already, meaning the limit has reset.
                    print "Had to wait %s seconds" % (amount_of_seconds_iterating) # This prints the amount of seconds in the iteration that it took.
                    amount_of_seconds_iterating= 2 # This just resets the iteration to 2 seconds for next time.
                client.open(google_sheet).sheet1 # This just opens the actual page.
                times_page_opened+=1
                sleep(.02)
                print times_page_opened # This helps me keep track of how many times the code has succesfully opened the page.
        except Exception as e:
            print "ERROR" # Tells me the code is waiting because it hit the error.
            amount_of_seconds_iterating*=2 # Doubles the waiting time.
            try:
                if re.search('"code": 429', str(e)): # Confirms that it is infact the resource limit error.
                    sleep(amount_of_seconds_iterating)
                    client.open(google_sheet).sheet1 # Attempts again to re-open the page.
            except:
                print "Double Error" 
    

    实施和简化后,它看起来像这样。

    def reset_limit():
        iteration= 2
        while True:
            try:
                client.open('Chem Sheet Summary Report DO NOT DELETE').sheet1
                return
            except:
                iteration*=2
                sleep(iteration)
    

    要使用它,我会尝试执行我遇到的任何操作,如果它达到资源限制,它将调用 reset_limit 函数,该函数仅在资源限制重置后才会返回,就像这样。

    while True:
        try:
            client.open(google_sheet).sheet1
        except Exception as e:
            if re.search('"code": 429', str(e)):
                reset_limit()
    

    【讨论】:

      猜你喜欢
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多