ragas icon indicating copy to clipboard operation
ragas copied to clipboard

使用 context manager 修复文件资源泄漏

Open YutShang opened this issue 3 months ago • 1 comments

问题描述

MetricAnnotation 与 SingleMetricAnnotation 类的 from_json 类方法中,读取 JSON 文件直接使用 json.load(open(path)),没有使用 with 上下文管理器。 文件打开之后没有显式关闭文件句柄,依赖 Python GC 垃圾回收释放文件资源;高频率批量读取文件场景下,会造成文件句柄泄漏,严重时触发系统打开文件数上限 Too many open files 异常。

问题代码片段

# MetricAnnotation.from_json (第 614 行)
dataset = json.load(open(path))

# SingleMetricAnnotation.from_json (第 646 行)
dataset = json.load(open(path))

YutShang avatar Sep 02 '26 14:09 YutShang

Hi, I'd like to work on this. I'll switch both json.load(open(path)) calls in MetricAnnotation.from_json and SingleMetricAnnotation.from_json to use with open(path) as f:, and run the existing tests. Will open a PR shortly.

abdd8433 avatar Oct 14 '26 11:10 abdd8433