我有一个大约 300K API URL 的列表,我想调用这些 URL 并从中获取数据:
lst = ['url.com','url2.com']
如果我将我的列表细分为 5 个 urlgrequest
可以完美地处理请求。但是,当我传入完整的 ~300K URL 时,出现错误:
Problem: url.Iam.passing.in: HTTPSConnectionPool(host='url', port=xxx): Max retries exceeded with url: url.Iam.passing.in (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x552b17550>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
Traceback (most recent call last):
到目前为止进行异步调用的代码:
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, stream=False) for u in self.urls), exception_handler=self.exception, size=5)
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)
当我通过时,我不确定自己做错了什么stream=False
。
无论如何我可以分批传递我的清单吗?