【发布时间】:2018-09-30 18:25:29
【问题描述】:
我正在编写一个辅助类,用于以异步方式处理多个 url 请求。代码如下。
class urlAsyncClient(object):
def __init__(self, url_arr):
self.url_arr = url_arr
async def async_worker(self):
result = await self.__run()
return result
async def __run(self):
pending_req = []
async with aiohttp.ClientSession() as session:
for url in self.url_arr:
r = self.__fetch(session, url)
pending_req.append(r)
#Awaiting the results altogether instead of one by one
result = await asyncio.wait(pending_req)
return result
@staticmethod
async def __fetch(session, url):
async with session.get(url) as response: #ERROR here
status_code = response.status
if status_code == 200:
return await response.json()
else:
result = await response.text()
print('Error ' + str(response.status_code) + ': ' + result)
return {"error": result}
因为在异步中一一等待结果似乎毫无意义。我将它们放入一个数组中,然后通过await asyncio.wait(pending_req) 一起等待。
但似乎这不是正确的方法,因为我收到以下错误
在 __fetch async 中使用 session.get(url) 作为响应:RuntimeError: Session is closed
我可以知道正确的方法吗?谢谢。
【问题讨论】:
标签: python python-3.x async-await aiohttp