![](/img/trans.png)
[英]Continuously hitting the GitHub secondary rate limit even after following the best practices?
[英]Why is my GitHub code search hitting secondary rate limits?
我正在搜索包含字符串“torch”的 GitHub 個文件。 由於搜索 API 將搜索限制為前 100 個結果,因此我根據此處建議的文件大小進行搜索。 但是,我一直在達到二級速率限制。 有人可以建議我是否做錯了什么,或者是否有辦法優化我的代碼以防止這些速率限制? 我已經看過處理速率限制的最佳實踐。 這是我的代碼 -
import os
import requests
import httplink
import time
# This for loop searches for code based on files sizes from 0 to 500000 containing the string "torch"
for i in range(0,500000,250):
print("i = ",i," i + 250 = ", i+250)
url = "https://api.github.com/search/code?q=torch +in:file + language:python+size:"+str(i)+".."+str(i+250)+"&page=1&per_page=10"
headers = {"Authorization": f'Token xxxxxxxxxxxxxxx'} ## Please put your token over here
# Backoff when secondary rate limit is reached
backoff = 256
total = 0
cond = True
# This while loop goes over all pages of results => Pagination
while cond==True:
try:
time.sleep(2)
res = requests.request("GET", url, headers=headers)
res.raise_for_status()
link = httplink.parse_link_header(res.headers["link"])
data = res.json()
for i, item in enumerate(data["items"], start=total):
print(f'[{i}] {item["html_url"]}')
if "next" not in link:
break
total += len(data["items"])
url = link["next"].target
# Except case to catch when secondary rate limit has been reached and prevent the computation from stopping
except requests.exceptions.HTTPError as err:
print("err = ", err)
print("err.response.text = ", err.response.text)
# backoff **= 2
print("backoff = ", backoff)
time.sleep(backoff)
# Except case to catch when the given file size provides no results
except KeyError as error:
print("err = ", error)
# Set cond to False to stop the while loop
cond = False
continue
基於這個答案,這似乎是一個普遍現象。 但是,我希望有人可以提出解決方法。
我添加了 Octokit 標簽,雖然我沒有使用它,以提高可見性,因為這似乎是一個常見問題。
上述邏輯/代碼的很大一部分是通過 SO 答案獲得的,我非常感謝社區的所有支持。
請注意,搜索的主要和次要速率限制低於其他限制。 對於 JavaScript,我們有一個節流插件,它實現了所有推薦的最佳實踐。 對於搜索,我們將請求限制為每 2 秒 1 個。 希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.