You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3.10.9容器中eventlet打补丁引发requests递归错误求助

解决eventlet monkey_patch后requests请求递归报错问题

问题场景

在Python 3.10.9的Docker容器中,应用依赖eventlet.monkey_patch(socket=True),调用requests.Session().get()时出现RecursionError: maximum recursion depth exceeded错误。复现代码如下:

import requests
from requests.adapters import HTTPAdapter

s = requests.Session()
s.mount('https://', HTTPAdapter(pool_connections=2))
s.get('https://stackoverflow.com/questions/34837026/whats-the-meaning-of-pool-connections-in-requests-adapters-httpadapter')
# <Response [200]>

import eventlet
eventlet.monkey_patch(socket=True)

s.get('https://stackoverflow.com/questions/34837026/whats-the-meaning-of-pool-connections-in-requests-adapters-httpadapter')
# <Response [200]>

s.get('https://www.google.com/')
# RecursionError: maximum recursion depth exceeded

s.get('https://stackoverflow.com/questions/34837026/whats-the-meaning-of-pool-connections-in-requests-adapters-httpadapter')
# <Response [200]>

可行兼容方案

方案1:提前执行monkey_patch

将eventlet.monkey_patch(socket=True)放在所有requests相关导入和Session初始化之前执行,让requests从一开始就使用eventlet打补丁后的socket实现,避免新旧逻辑冲突。修改后代码:

import eventlet
eventlet.monkey_patch(socket=True)

import requests
from requests.adapters import HTTPAdapter

s = requests.Session()
s.mount('https://', HTTPAdapter(pool_connections=2))
s.get('https://stackoverflow.com/questions/34837026/whats-the-meaning-of-pool-connections-in-requests-adapters-httpadapter')
s.get('https://www.google.com/')  # 正常返回响应

方案2:打补丁后重置HTTPAdapter

如果无法提前执行monkey_patch,可在打补丁后重新创建并挂载HTTPAdapter,替换掉之前绑定原生socket的旧适配器:

import requests
from requests.adapters import HTTPAdapter

s = requests.Session()
s.mount('https://', HTTPAdapter(pool_connections=2))
s.get('https://stackoverflow.com/questions/34837026/whats-the-meaning-of-pool-connections-in-requests-adapters-httpadapter')

import eventlet
eventlet.monkey_patch(socket=True)

# 重置适配器,让Session使用补丁后的socket
s.mount('https://', HTTPAdapter(pool_connections=2))

s.get('https://www.google.com/')  # 正常执行
s.get('https://stackoverflow.com/questions/34837026/whats-the-meaning-of-pool-connections-in-requests-adapters-httpadapter')  # 正常执行

问题本质

递归错误源于:monkey_patch前,HTTPAdapter已初始化并绑定了原生socket的连接逻辑;打补丁后eventlet替换了socket实现,但旧适配器仍在调用原生方法,而原生方法被补丁逻辑劫持,形成循环调用,最终触发递归深度超限。

内容的提问来源于stack exchange,提问作者oliver nadj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:17:32