I was getting some problems with my code. I have a aiohttp client session, which communicate with a website via requests.
The problem is, when I run the code for long time, I start to get some errors like ClientResponseError, ServerDisconnectedError, Error 101. So I was reading the docs and I saw this:
release()
Release connection back to connector.
Underlying socket is not closed, the connection may be reused later if timeout (30 seconds by default) for connection was not expired.
But I didn't understand. Can someone explain it simple way please? Would it solve my problem?
session = aiohttp.ClientSession(cookie_jar=cookiejar)
while True:
await session.post('https://anywhere.com', data={'{}': ''})
The exception is raised when the server you connect to closed the connection prematurely. It happens. However, this is not something that releasing the connection to the pool will fix, and the code you posted already releases the connection, albeit implicitly. Instead, you need to handle the exception, it is your application that needs to decide how to handle this error.
You may want to use the response object as a context manager, which will help with releasing connections earlier when you don't need to have access to the response data any more. Your sample code doesn't use the return value of the session.post() coroutine, so the connection is already automatically released for you when Python deletes it from memory (which happens when there are no references left to it), but using it as a context manager lets Python know you no longer need it by being explicit.
Here is a simple version using (asynchronous) context managers which catches the exception thrown on server disconnect, and more:
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")
I chose to catch ClientConnectionError, which is a base class that ServerDisconnectedError is derived from, but catching this exception lets you handle more connection error cases with the same exception handler. See the exception hierarchy to help you decide what exceptions to catch, it'll depend on how much detail you feel you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With