switch request_many() to use asyncio.gather

This commit is contained in:
jesopo 2019-07-08 13:46:27 +01:00
parent c6c60a5192
commit ee0ec0eca1

View file

@ -116,8 +116,7 @@ def request(url: str, method: str="GET", get_params: dict={},
def request_many(urls: typing.List[str]) -> typing.Dict[str, Response]:
responses = {}
async def _request():
for url in urls:
async def _request(url):
client = tornado.httpclient.AsyncHTTPClient()
request = tornado.httpclient.HTTPRequest(url, method="GET",
connect_timeout=2, request_timeout=2)
@ -133,7 +132,12 @@ def request_many(urls: typing.List[str]) -> typing.Dict[str, Response]:
responses[url] = Response(response.code, data, headers)
loop = asyncio.get_event_loop()
loop.run_until_complete(_request())
awaits = []
for url in urls:
awaits.append(_request(url))
task = asyncio.gather(*awaits, return_exceptions=True)
loop.run_until_complete(task)
return responses
def strip_html(s: str) -> str: