In the aiohttp's doc reads:
- loop – event loop used for processing HTTP requests. If loop is None the constructor borrows it from connector if specified. asyncio.get_event_loop() is used for getting default event loop otherwise.
Deprecated since version 2.0.
I googled but didn't get any description about why the loop
parameter is deprecated.
I often create ClientSession
object like this:
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession(loop=loop)
Now the loop
parameter is depracted, but just call aiohttp.ClientSession()
without the loop will get a warning:
Creating a client session outside of coroutine
So why the parameter is deprecated and how to use the session correctly?
This question is addressed in this issue which is advising that the client session object be created within a coroutine to avoid errors that are difficult to debug. The preferred usage is demonstrated here; for reference:
async def fetch(client):
async with client.get('http://python.org') as resp:
assert resp.status == 200
return await resp.text()
async def main():
async with aiohttp.ClientSession() as client:
html = await fetch(client)
print(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
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