Python requests连接池超出错误urllib3.connectionpool:Connection pool is full discarding connection: xxx 错误处理

Ruan's blog / 2024-01-22 / 原文

今天在进行多线程请求的时候出现问题,但是是警告,不过会导致把其他请求给关闭掉,严重影响效率,在网上搜了一大堆都是说urllib3的,没有说requests的。

WARNING:urllib3.connectionpool:Connection pool is full, discarding connection: 450632824.shop.n.weimob.com. Connection pool size: 10

先说解决方案

import requests 
session = requests.Session()
 # 设置连接池数量为100
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100) 
session.mount('http://', adapter) 
session.mount('https://', adapter)
# 设置完成后再多线程请求,就不会很容易爆出警告了

requests官方文档-HTTPAdapter 中的说明,其实本质上就是urllib3的HTTP连接池

image