Search code examples
pythonpython-3.xpython-asyncioaiohttp

aiohtttp connection pooling with ProxyConnector


I was wondering if anyone knows how to do connection pooling with aiohttp using the ProxyConnector as the connector?

The docs mention how to do this with the TcpConnector or using the Session class, but I can not seem to figure it out.

Thanks.


Solution

  • The ClientSession object, which handles connection pooling, takes a connector keyword argument:

    class aiohttp.client.ClientSession(*, connector=None, loop=None, request_class=None, response_class=None, cookies=None, headers=None, auth=None)

    if no connector is given, a TCPConnector will be used by default, but you can just specify a ProxyConnector instance, and that will be used instead. You can see this logic in the source:

    class ClientSession:
    
        def __init__(self, *, connector=None, loop=None, request_class=None,
                     response_class=None, cookies=None, headers=None, auth=None):
            if loop is None:
                loop = asyncio.get_event_loop()
            self._loop = loop
            self.cookies = http.cookies.SimpleCookie()
            if connector is None:
                connector = aiohttp.TCPConnector(force_close=True, loop=loop)