My code:
import aiohttp
import asyncio
connector = aiohttp.TCPConnector(limit=1)
async def get(url):
print('started thread', url)
session = aiohttp.ClientSession(connector=connector)
response = await session.get(url, timeout=5)
await session.close()
print(await response.text())
loop = asyncio.get_event_loop()
tasks = [
loop.create_task(get('http://httpbin.org/ip')),
loop.create_task(get('http://httpbin.org/ip')),
loop.create_task(get('http://httpbin.org/ip')),
loop.create_task(get('http://httpbin.org/ip')),
loop.create_task(get('http://httpbin.org/ip')),
loop.create_task(get('http://httpbin.org/ip')),
]
loop.run_until_complete(asyncio.wait(tasks))
connector.close()
After 1 request, i get error: aiohttp.client_exceptions.ClientConnectionError: Connector is closed.
This error happens when you try to make a request on a connector that you've already closed. You're calling session.close() in your get function, which in turn is closing its connector. This would normally be fine, but since you're initializing one connector and explicitly passing it to each session, it is being used by all of them.
As soon as one of the requests finishes, session.close() closes the shared connector, causing all of the other active requests to immediately fail with the error you're seeing.
There's a bunch of ways to alleviate this, depending on what you're trying to do:
close on it until after run_until_complete (where you're currently trying to close the connector)get and then close them all after.I'm not sure what your use case is, but if #1 and #2 really don't work, you may want to check out semaphores.
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