我正在使用 grequests 库传递约 250000 个 url 以从 api 获取数据。
API 的调用限制为每秒 100 次。
如何将请求限制为每秒仅传递 100 个 URL?我将大小参数从 5 增加到 100。不确定这会做什么,但仍然会出现错误“超出最大重试次数”。
这是迄今为止我的代码:
import grequests
lst = ['url.com','url2.com']
class Test:
def __init__(self):
self.urls = lst
def exception(self, request, exception):
print ("Problem: {}: {}".format(request.url, exception))
def async(self):
return grequests.map((grequests.get(u) for u in self.urls), exception_handler=self.exception, size=100)
def collate_responses(self, results):
return [x.text for x in results]
test = Test()
#here we collect the results returned by the async function
results = test.async()
response_text = test.collate_responses(results)
最佳答案
Grequests 似乎发出了 100 个请求,然后在没有任何等待的情况下发出了另外 100 个请求,依此类推。这些请求之间没有定义时间。 这是一个类似的问题及其解决方案: Limiting/throttling the rate of HTTP requests in GRequests
关于python - 进行异步 API 调用时如何限制 Grequests?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52005901/