-
This issue was previously mentioned, see #4015. If you do this, it just works fine. import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
response = await session.get("https://docs.aiohttp.org/en/stable/")
print("Status:", response.status)
print("Content-type:", response.headers['content-type'])
html = await response.text()
print("Body:", html[:16], "...")
loop = asyncio.get_event_loop()
loop.run_until_complete(main()) But if you try to read the response outside of the async with block, it will often fail. import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
response = await session.get("https://docs.aiohttp.org/en/stable/")
print("Status:", response.status)
print("Content-type:", response.headers['content-type'])
html = await response.text()
print("Body:", html[:16], "...")
loop = asyncio.get_event_loop()
loop.run_until_complete(main()) What is interesting is that it does not always fail. If you try to run this code several times, then it is likely that at least one time it will also read the response. Does anyone know what causes it? |
Beta Was this translation helpful? Give feedback.
Answered by
webknjaz
Jun 19, 2021
Replies: 1 comment 2 replies
-
Async context manager makes sure to release the resources. You shouldn't access them outside of it. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
webknjaz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Async context manager makes sure to release the resources. You shouldn't access them outside of it.