Python urllib3:一段时间后关闭空闲连接 [英] Python urllib3: close idle connection after some time

查看:106
本文介绍了Python urllib3:一段时间后关闭空闲连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉Python urllib3在一段时间后不要重用空闲连接,而是关闭它们?

Is there a way to tell Python urllib3 to not reuse idle connections after some period of time, and instead to close them?

查看 https://urllib3.readthedocs.io/en/latest/reference/index.html#module-urllib3.connectionpool 似乎没有显示任何相关内容.

Looking in https://urllib3.readthedocs.io/en/latest/reference/index.html#module-urllib3.connectionpool doesn't seem to show anything relevant.

推荐答案

记住:

连接池是维护数据库连接的缓存,因此当将来向服务器发送请求时,可以重用"连接数据库是必需的.

A connection pool is a cache of database connections maintained so that the connections can be "reused" when future requests to the database are required.

您可以通过多种方式执行此操作(我想):

You can do this is many ways (I guess):

  • 将重试次数设置为1.

如果一次失败,这会中断您的连接.要设置它:

This breaks your connection if it failed one time. To set it:

import requests
s = requests.Session()
a = requests.adapters.HTTPAdapter(max_retries=1) # is zero for default
s.mount('http://', a)


  • 更改池连接.
  • "pool_connections"是要保留的主机池数.例如,如果您要连接到100个不同的主机,并且 pool_connections = 10 ,那么将仅重用最近的10个主机的连接.设置:

    The "pool_connections" is the number of host-pools to keep around. For example, if you're connecting to 100 different hosts, and pool_connections=10, then only the latest 10 hosts' connections will be re-used. To set that:

    s = requests.Session()
    s.mount('https://', HTTPAdapter(pool_connections=1))
    s.get('https://www.example.com')
    

    这将停止池的重用.

    • 泳池最大尺寸

    仅当您在多线程环境中使用Session时,才可以解决此问题.要设置它:

    This is cared only if you use Session in a multithreaded environment. To set it:

    s = requests.Session()
    s.mount('https://', HTTPAdapter(pool_connections=1, pool_maxsize=1))
    


    • 配置最大大小
    • he:class:〜connectionpool.ConnectionPool 类保留一个单独的:class:〜connection.HTTPConnection 实例的池.这些连接在单个请求期间使用,并在请求完成后返回到池中.默认情况下,将仅保存一个连接以供重用.要设置它(默认情况下是):

      he :class:~connectionpool.ConnectionPool class keeps a pool of individual :class:~connection.HTTPConnection instances. These connections are used during an individual request and returned to the pool when the request is complete. By default only one connection will be saved for re-use. To set it (it is, by default):

      from urllib3 import HTTPConnectionPool
      pool = HTTPConnectionPool('www.example.com', maxsize=0) #likely to slow you down cuz it never stores the pools
      

      maxsize –可以重复使用的保存连接数.在多线程情况下,大于1是有用的.

      maxsize – Number of connections to save that can be reused. More than 1 is useful in multithreaded situations.

      • 让池管理器做到这一点!

      PoolManager使用最近最少使用(LRU)策略来丢弃旧池.也就是说,如果将PoolManager num_pools 设置为10,则在向11个或更多不同的主机发出请求后,将最终清理最近最少使用的池.为此:

      The PoolManager uses a Least Recently Used (LRU) policy for discarding old pools. That is, if you set the PoolManager num_pools to 10, then after making requests to 11 or more different hosts, the least recently used pools will be cleaned up eventually. So to do that:

      from urllib3 import PoolManager
      manager = PoolManager(1) # not the manager cleans up pools used for one time
      r = manager.request('GET', 'http://www.example.com/')
      

      此外,文档说:

      清除陈旧的池不会立即发生.

      Cleanup of stale pools does not happen immediately.

      为此,请使用 RecentlyUsedContainer (文档只包含一行).

      So for that Use RecentlyUsedContainer (Docs contains only one line).

      注意:

      设置参数,如果PoolManager影响所有连接的池从而.

      Setting arguments if PoolManager affects all the pools connected thereby.


      希望这对您有帮助.获取高级用法文档 HERE .

      这篇关于Python urllib3:一段时间后关闭空闲连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆