Created
June 8, 2020 08:06
-
-
Save seozed/097dd9f7f25924645ae8be60f5e2cec2 to your computer and use it in GitHub Desktop.
[限制客户端请求速度的装饰器] #qps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ratelimit import limits, sleep_and_retry | |
import requests | |
FIFTEEN_MINUTES = 900 | |
@sleep_and_retry | |
@limits(calls=15, period=FIFTEEN_MINUTES) | |
# 900秒内最多请求15次。 | |
def call_api(url): | |
response = requests.get(url) | |
if response.status_code != 200: | |
raise Exception('API response: {}'.format(response.status_code)) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment