How to close requests.Session()?

11,212

In requests's source code, Session.close only close all underlying Adapter. And further closing a Adapter is clearing underlying PoolManager. Then all the established connections inside this PoolManager will be closed. But PoolManager will create a fresh connection if there is no usable connection.

Critical code:

# requests.Session
def close(self):
    """Closes all adapters and as such the session"""
    for v in self.adapters.values():
        v.close()

# requests.adapters.HTTPAdapter
def close(self):
    """Disposes of any internal state.

    Currently, this closes the PoolManager and any active ProxyManager,
    which closes any pooled connections.
    """
    self.poolmanager.clear()
    for proxy in self.proxy_manager.values():
        proxy.clear()

# urllib3.poolmanager.PoolManager
def connection_from_pool_key(self, pool_key, request_context=None):
    """
    Get a :class:`ConnectionPool` based on the provided pool key.

    ``pool_key`` should be a namedtuple that only contains immutable
    objects. At a minimum it must have the ``scheme``, ``host``, and
    ``port`` fields.
    """
    with self.pools.lock:
        # If the scheme, host, or port doesn't match existing open
        # connections, open a new ConnectionPool.
        pool = self.pools.get(pool_key)
        if pool:
            return pool

        # Make a fresh ConnectionPool of the desired type
        scheme = request_context['scheme']
        host = request_context['host']
        port = request_context['port']
        pool = self._new_pool(scheme, host, port, request_context=request_context)
        self.pools[pool_key] = pool

    return pool

So if I understand its structure well, when you close a Session, you are almost the same as creating a new Session and assign it to old one. So you can still use it to send request.

Or if I misunderstand anything, welcome to correct me :D

Share:
11,212
phanny
Author by

phanny

BY Day: AI developer for chatbots

Updated on June 27, 2022

Comments

  • phanny
    phanny almost 2 years

    I am trying to close a requests.Session() but its not getting closed.

    s = requests.Session()
    s.verify = 'cert.pem'
    res1 = s.get("https://<ip>:<port>/<route>")
    s.close() #Not working
    res2 = s.get("https://<ip>:<port>/<route>") # this is still working which means s.close() didn't work.
    

    How do I close the session? s.close() is not throwing any error also which means it is a valid syntax but I am not understanding what exactly it is doing.

  • Kalyanam Rajashree
    Kalyanam Rajashree about 6 years
    But PoolManager will create a fresh connection if there is no usable connection --> When does this happen?
  • Sraw
    Sraw about 6 years
    When you create a fresh PoolManager, it just hold an empty pools. Or when you clear a PoolManager, it will clear pools.