logging: Logger.setLevel() and logging.disable() are O(number of loggers)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 38/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- python
- 领域
- backend, performance
调研方向
Start with Manager._clear_cache() and Logger.isEnabledFor() in CPython’s logging implementation, then inspect the logging tests that cover cache invalidation. The issue includes a benchmark reproducing quadratic behavior across thousands of loggers. Done means cache clearing scales with loggers that have cached values while preserving correct isEnabledFor() results after level changes.
由索引模型根据 Issue 内容生成。
描述
Bug report
Every Logger.setLevel() and logging.disable() call goes through Manager._clear_cache(), which walks the whole loggerDict and clears each logger's isEnabledFor cache, even the ones that never cached anything. So setting the level on N loggers one by one is quadratic. That shows up in apps or test suites with thousands of loggers (one per module/plugin/connection) that get configured individually.
import logging, time
for n in (1000, 2000, 4000, 8000):
logging.root.manager.loggerDict.clear()
loggers = [logging.getLogger(f"app.mod{i}") for i in range(n)]
for lg in loggers:
lg.isEnabledFor(logging.DEBUG)
t = time.perf_counter()
for lg in loggers:
lg.setLevel(logging.INFO)
print(f"{n:5d} loggers: {time.perf_counter() - t:.3f} s")
On 3.14.7 this gives about 0.23 s at 2000 loggers, 0.89 s at 4000 and 4.4 s at 8000 (each doubling is about 4x).
A fix is to have the manager remember which loggers actually have something in their cache (a WeakSet filled on the isEnabledFor cache-miss path, which already holds the lock) and only clear those. The cache-hit path doesn't change. I have a patch ready and can open a PR.
CPython versions tested on:
3.14, CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-160937
- 主要语言
- Python
- 星标
- 77.8k
- 派生
- 37.7k
- 平均合并
- 17 小时 24 分钟
- 30 天内合并 PR
- 716
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
build OS-unsupported OS-windows type-bug
难度 2/5 1-3 小时 新手友好度 60/100
维护者通常 1 天内回复
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689可能已有人在做 @TheLouisMahdi 于 1 天前认领。 未关闭docs
难度 1/5 1 小时以内 新手友好度 82/100
python/cpython#161078 · 3 条评论 ·
维护者通常 1 天内回复
-
OS-windows type-bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
docs
难度 2/5 1-3 小时 新手友好度 72/100
python/cpython#161007 · 4 条评论 ·
维护者通常 1 天内回复
-
Null dereferencing of a pointer in initconfig.c可能已有人在做 @lazerg 于 4 天前认领。 未关闭interpreter-core topic-C-API type-bug
难度 2/5 1-3 小时 新手友好度 72/100
python/cpython#160962 · 4 条评论 ·
维护者通常 1 天内回复
相似的 Issue
-
难度 1/5 1-3 小时 新手友好度 78/100
QuantConnect/Documentation#2838 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
awslabs/route53-dynamic-dns-with-lambda#67 · 1 条评论 ·
-
`SubAgents` returns a child run's `DeferredToolRequests` to the parent as a successful tool result未关闭bug pkg:harness
难度 2/5 1-3 小时 新手友好度 70/100
pydantic/pydantic-ai#12405 ·
维护者通常 1 天内回复
-
good first issue help wanted
难度 2/5 1-3 小时 新手友好度 78/100
-
good first issue help wanted
难度 1/5 1 小时以内 新手友好度 82/100