Del*_*lsx 0 python python-asyncio aiohttp
我的代码出现了一些问题。我有一个aiohttp客户端会话,它通过请求与网站进行通信。
问题是,当我长时间运行代码时,我开始收到一些错误,例如ClientResponseError, ServerDisconnectedError, Error 101。所以我正在阅读文档,我看到了这个:
release()
将连接释放回连接器。
底层套接字未关闭,如果连接超时(默认为 30 秒)未过期,则稍后可以重用连接。
但我不明白。有人可以简单地解释一下吗?它会解决我的问题吗?
session = aiohttp.ClientSession(cookie_jar=cookiejar)
while True:
await session.post('https://anywhere.com', data={'{}': ''})
Run Code Online (Sandbox Code Playgroud)
当您连接的服务器过早关闭连接时会引发异常。它发生了。但是,这不是释放与池的连接将解决的问题,并且您发布的代码已经释放了连接,尽管是隐式的。相反,您需要处理异常,您的应用程序需要决定如何处理此错误。
您可能希望将响应对象用作上下文管理器,这将有助于在您不再需要访问响应数据时更早地释放连接。您的示例代码不使用session.post()协程的返回值,因此当 Python 从内存中删除它时,连接已经自动为您释放(这种情况发生在没有剩余引用时),但是将它用作上下文管理器可以让Python 通过显式知道您不再需要它。
这是一个使用(异步)上下文管理器的简单版本,它捕获服务器断开连接时抛出的异常,等等:
with aiohttp.ClientSession(cookie_jar=cookiejar) as session:
while True:
try:
async with session.post('https://anywhere.com', data={'{}': ''}) as response:
# do something with the response if needed
# here, the async with context for the response ends, and the response is
# released.
except aiohttp.ClientConnectionError:
# something went wrong with the exception, decide on what to do next
print("Oops, the connection was dropped before we finished")
except aiohttp.ClientError:
# something went wrong in general. Not a connection error, that was handled
# above.
print("Oops, something else went wrong with the request")
Run Code Online (Sandbox Code Playgroud)
我选择了 catch ClientConnectionError,它是一个ServerDisconnectedError派生自的基类,但是捕获此异常可以让您使用相同的异常处理程序处理更多的连接错误情况。查看异常层次结构以帮助您决定要捕获哪些异常,这取决于您认为需要多少细节。