好的,所以我在阅读并给出了几个小时来运行不同的代码之后想通了。所以this post 真的帮助了我,因为我还注意到 Google 的帖子不一致。但答案指出了我需要思考的问题。
“关于算法的重要部分是它的指数,而不是尝试次数。”
Google 的文档说要在 16 秒后停止并记录下来。
所以我尝试了一些答案,
在尝试重新加载页面之前,我开始增加睡眠时间。甚至长达 20 分钟,有时它仍然无法正常工作。但有时 30 秒就足够了。
我尝试按照文档中的建议在睡眠量上添加随机秒数,以使其保持不同和随机,但我相信这更适用于网络,而不是一个程序和一张纸通信,没有其他预计会干扰的机器。
我尝试更改页面,可能我打开的页面已被锁定,因为我达到了它的资源限制。
我基本上做了一个小的 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()