Why ClientSession in async context manager freeze until timeout approximately once in 3-4 runs?
When I await any coroutine from response inside async context manager everything works normal but if I try to do it outside (or return response) it freeze?
Here is minimal representation of code
from aiohttp import ClientSession
import asyncio
async def fetch():
url = ...
headers = {"User-Agent": ...}
parameters = [...]
async with ClientSession() as session:
session.headers.update(headers)
response = await session.post(url, data=parameters, ssl=False)
# await response.json() # <-- This line fix problem
return response
async def main():
r = await fetch()
print(await r.json())
asyncio.run(main())
Problem fixed by returning response inside async context manager but I want to know why this problem appears.
Here's what happens when the code runs outside of async with
:
When you return response outside of context, it's not awaited and consumed, so it doesn't work.
When you return the response from inside the async with
context, the response is consumed and awaited, so it works fine.