如何在进行异步 API 调用时限制 Grequest?
How to throttle Grequests when making asynchronous API calls?
我正在使用 grequests 库传递约 250000 个 url 以从 api 获取数据。
API 有每秒 100 次调用的限制。
如何限制 grequest 每秒仅传递 100 个 url?我将大小参数从 5 增加到 100。不确定这是做什么的,但仍然 运行 错误 'Max retries exceeded'。
到目前为止,这是我的代码:
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 个请求,依此类推。这些请求之间没有定义时间。
这是用解决方案描述的类似问题:
我正在使用 grequests 库传递约 250000 个 url 以从 api 获取数据。
API 有每秒 100 次调用的限制。
如何限制 grequest 每秒仅传递 100 个 url?我将大小参数从 5 增加到 100。不确定这是做什么的,但仍然 运行 错误 'Max retries exceeded'。
到目前为止,这是我的代码:
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 个请求,依此类推。这些请求之间没有定义时间。 这是用解决方案描述的类似问题: