Python导入ClinicalTrials.gov XML到MongoDB时触发MemoryError
我之前帮好几个开发者解决过类似的批量小文件导入MongoDB内存溢出问题,你的情况大概率是一次性加载太多文件到内存,或者MongoDB写入缓存/连接配置没做优化导致的。下面是几个实用的解决方案,按优先级排序:
1. 分批处理文件,避免一次性加载全部
不要把所有文件路径或内容一次性读入内存,改用迭代器分批遍历+处理,处理完一批就释放内存。比如用Python实现:
import os import glob import gc from pymongo import MongoClient import xmltodict client = MongoClient() db = client['clinical_trials'] collection = db['trials'] # 每批处理1000个文件,可根据服务器内存调整 batch_size = 1000 file_paths = glob.glob(os.path.join(dirpath_zip, '*.xml')) for i in range(0, len(file_paths), batch_size): batch_files = file_paths[i:i+batch_size] for file_path in batch_files: try: # 用with语句自动关闭文件,及时释放资源 with open(file_path, 'r') as f: xml_content = xmltodict.parse(f.read()) collection.insert_one(xml_content) except Exception as e: print(f"处理文件 {file_path} 失败: {str(e)}") # 每批处理完强制触发垃圾回收,释放闲置内存 gc.collect()
2. 优化MongoDB写入逻辑,减少内存缓存
默认的单条插入会产生较多连接开销和内存缓存,改用批量插入能大幅降低内存占用:
import os import glob import gc from pymongo import MongoClient import xmltodict client = MongoClient(maxPoolSize=10) # 调小连接池,减少闲置连接内存占用 db = client['clinical_trials'] collection = db['trials'] batch_size = 1000 batch_data = [] file_paths = glob.glob(os.path.join(dirpath_zip, '*.xml')) for file_path in file_paths: try: with open(file_path, 'r') as f: xml_content = xmltodict.parse(f.read()) batch_data.append(xml_content) # 攒够一批就批量写入 if len(batch_data) >= batch_size: collection.insert_many(batch_data) batch_data = [] gc.collect() except Exception as e: print(f"处理文件 {file_path} 失败: {str(e)}") # 处理最后一批剩余数据 if batch_data: collection.insert_many(batch_data)
3. 系统层面补充内存缓冲
如果你的DigitalOcean服务器没有启用交换分区,临时创建swap可以缓解内存不足的问题:
# 创建1GB交换文件,可根据需求调整大小 sudo fallocate -l 1G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
另外,也可以限制Python进程的内存上限,避免直接触发系统OOM Killer:
ulimit -v 2097152 # 限制进程最多使用2GB内存
4. 检查异常文件
有时候单个异常大文件会突然撑爆内存,先定位第15988个文件看看是否异常:
ls -lh /path/to/your/files/[第15988个文件名].xml
如果这个文件远大于10KB,建议单独处理(比如跳过)。另外,改用SAX流式XML解析器代替DOM解析(比如xml.sax),它不会把整个XML树加载到内存,内存占用会更低。
内容的提问来源于stack exchange,提问作者akondo
相关产品推荐
相关产品推荐

