python requests连接池

1、下面两种方法是相同的,requests都会默认建立10个连接数上限制是10的连接池

import requests

#方法1
s = requests.Session()
s.get('https://www.baidu.com')

#方法2
requests.get('https://www.baidu.com')

2、其中涉及到的几个参数

pool_connections=DEFAULT_POOLSIZE,
pool_maxsize=DEFAULT_POOLSIZE, 
max_retries=DEFAULT_RETRIES,
pool_block=DEFAULT_POOLBLOCK
"""
:param pool_connections: The number of urllib3 connection pools to cache.
:param pool_maxsize: The maximum number of connections to save in the pool.
:param max_retries: The maximum number of retries each connection
        should attempt. Note, this applies only to failed DNS lookups, socket
        connections and connection timeouts, never to requests where data has
        made it to the server. By default, Requests does not retry failed
        connections. If you need granular control over the conditions under
        which we retry a request, import urllib3's ``Retry`` class and pass
        that instead.
:param pool_block: Whether the connection pool should block for connections.
"""
#默认值
DEFAULT_POOLBLOCK = False
DEFAULT_POOLSIZE = 10
DEFAULT_RETRIES = 0

(1)一个host对应一个连接池,默认一个连接池同时支持10个链接,当超过限制时,由pool_block参数决定是否阻塞等待。

(2)也可以通Sessio的mount函数和HTTPAdapter修改默认配置。

 

3、参考:

https://www.cnblogs.com/pengyusong/p/5802929.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值