gpt4 book ai didi

python-3.x - xml.etree.ElementTree.iterparse() 对于大型 XML 文档不可扩展?

转载 作者:行者123 更新时间:2023-12-01 01:01:25 24 4
gpt4 key购买 nike

我正在尝试通过一个 XML 文档的 iterparse() 进行增量解析,该文档(设计为)太大而无法放入内存。我发现即使对文档执行无操作传递也会耗尽进程内存并导致我的系统开始交换。

期望 xml.etree.ElementTree.iterparse() 在与 XML 文档大小无关的常量内存中运行是错误的吗?如果是这样,用于对任意长的 XML 文档进行增量解析的推荐包是什么?如果没有,WTF 是我的代码有问题吗?

这是代码:
请注意,我仅请求“开始”事件(因此解析器不会在返回文档根元素的结束标记(在我的情况下为 )之前尝试缓冲所有正文元素。并且我明确地将循环变量 del()强制他们被释放。

考虑到垃圾收集器可能没有机会运行,因为循环没有产生,我每百万次迭代添加了对 gc.collect()time.sleep() 的显式调用。但它没有帮助。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xml.etree.ElementTree as ET
import pprint

import gc
import time
import os
import psutil

def gcStats(myProc):
# return human readable gc.stats for 3 generations

extmem = myProc.memory_info_ex()

a = "extmem: rss {:12n}, vms {:12n}, shared{:12n}, text{:12n}, lib {:12n}, data{:12n}, dirty{:12n}".format(
extmem.rss, extmem.vms, extmem.shared, extmem.text, extmem.lib, extmem.data, extmem.dirty)

return a + "\tgc enabled {}, sumCount {:n}, lenGarbage {:n}".format( gc.isenabled(), sum(gc.get_count()), len(gc.garbage))

# the misbehaving code:
def count_tags(filename):
retVal = {}
iterCount = 0
sleepTime = 2.0
myProc = psutil.Process()

print("Starting: gc.isenabled() == {}\n{}".format(gc.isenabled(), gcStats( myProc)))

for event, element in ET.iterparse(filename, ('start',)):
assert event == 'start'
if iterCount % 1000000 == 0:
print('{} iterations, sleeping {} sec...'.format(iterCount, sleepTime))
time.sleep( sleepTime)
print('{}\nNow starting gc pass...'.format( gcStats( myProc)))
gcr = gc.collect()
print('gc returned {}'.format( gcr))
iterCount += 1
del element
del event
return retVal


if __name__ == "__main__":
tags = count_tags('/home/bobhy/MOOC_Data/' + 'chicago.osm')

这是文档的示例。它是格式良好的 OSM 数据。
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="Osmosis 0.43.1">
<bounds minlon="-88.50500" minlat="41.33900" maxlon="-87.06600" maxlat="42.29700" origin="http://www.openstreetmap.org/api/0.6"/>
<node id="219850" version="54" timestamp="2011-04-06T05:17:15Z" uid="207745" user="NE2" changeset="7781188" lat="41.7585879" lon="-87.9101245">
<tag k="exit_to" v="Joliet Road"/>
<tag k="highway" v="motorway_junction"/>
<tag k="ref" v="276C"/>
</node>
<node id="219851" version="47" timestamp="2011-04-06T05:18:47Z" uid="207745" user="NE2" changeset="7781188" lat="41.7593116" lon="-87.9076432">
<tag k="exit_to" v="North I-294 ; Tri-State Tollway; Wisconsin"/>
<tag k="highway" v="motorway_junction"/>
<tag k="ref" v="277A"/>
</node>
<node id="219871" version="1" timestamp="2006-04-15T00:34:03Z" uid="229" user="LA2" changeset="3725" lat="41.932278" lon="-87.9179332"/>
<node id="700724" version="14" timestamp="2009-04-13T11:21:51Z" uid="18480" user="nickvet419" changeset="485405" lat="41.7120272" lon="-88.0158606"/>

. . .等等 1.8 GB 。 . .
  <relation id="3366425" version="1" timestamp="2013-12-07T21:37:35Z" uid="239998" user="Sundance" changeset="19330301">
<member type="way" ref="250651738" role="outer"/>
<member type="way" ref="250651748" role="inner"/>
<tag k="type" v="multipolygon"/>
</relation>
<relation id="3378994" version="1" timestamp="2013-12-14T22:24:26Z" uid="371121" user="AndrewSnow" changeset="19456337">
<member type="way" ref="251850076" role="outer"/>
<member type="way" ref="251850073" role="inner"/>
<member type="way" ref="251850074" role="inner"/>
<member type="way" ref="251850075" role="inner"/>
<tag k="type" v="multipolygon"/>
</relation>
<relation id="3382796" version="1" timestamp="2013-12-17T03:21:18Z" uid="567034" user="Umbugbene" changeset="19492258">
<member type="way" ref="252225400" role="outer"/>
<member type="way" ref="252225404" role="inner"/>
<tag k="type" v="multipolygon"/>
</relation>
</osm>

这是输出:
Starting: gc.isenabled() == True
extmem: rss 9097216, vms 37199872, shared 3145728, text 3301376, lib 0, data 5820416, dirty 0 gc enabled True, sumCount 410, lenGarbage 0
0 iterations, sleeping 2.0 sec...
extmem: rss 9097216, vms 37335040, shared 3145728, text 3301376, lib 0, data 5955584, dirty 0 gc enabled True, sumCount 87, lenGarbage 0
Now starting gc pass...
gc returned 0
1000000 iterations, sleeping 2.0 sec...
extmem: rss 1234309120, vms 1262891008, shared 3280896, text 3301376, lib 0, data 1231511552, dirty 0 gc enabled True, sumCount 372, lenGarbage 0
Now starting gc pass...
gc returned 0
2000000 iterations, sleeping 2.0 sec...
extmem: rss 2495262720, vms 2524073984, shared 3280896, text 3301376, lib 0, data 2492694528, dirty 0 gc enabled True, sumCount 37, lenGarbage 0
Now starting gc pass...
gc returned 0
3000000 iterations, sleeping 2.0 sec...
extmem: rss 3781947392, vms 3812208640, shared 3280896, text 3301376, lib 0, data 3780829184, dirty 0 gc enabled True, sumCount 262, lenGarbage 0
Now starting gc pass...
gc returned 0
4000000 iterations, sleeping 2.0 sec...
extmem: rss 5067837440, vms 5096787968, shared 3280896, text 3301376, lib 0, data 5065408512, dirty 0 gc enabled True, sumCount 241, lenGarbage 0
Now starting gc pass...
gc returned 0
5000000 iterations, sleeping 2.0 sec...
extmem: rss 6345998336, vms 6375632896, shared 3063808, text 3301376, lib 0, data 6344253440, dirty 0 gc enabled True, sumCount 333, lenGarbage 0
Now starting gc pass...
gc returned 0
6000000 iterations, sleeping 2.0 sec...
extmem: rss 7266795520, vms 7665147904, shared 1060864, text 3301376, lib 0, data 7633768448, dirty 0 gc enabled True, sumCount 877, lenGarbage 0
Now starting gc pass...

我解释输出以显示进程虚拟内存以大约 1 000 B/迭代的速度增长(即,每个解析的 XML 标记)。我认为垃圾收集统计数据没有显示分配对象的单调增加,所以我不知道内存增长来自哪里。确实启用了垃圾收集。

最佳答案

您需要通过调用方法 element.clear() 来明确清除不再需要的元素。 ,否则它仍然会留在内存中。这意味着您可能还想听 'end'事件及调用 clear()一旦到达封装元素的末尾,您就知道不再需要它的任何内容。

关于python-3.x - xml.etree.ElementTree.iterparse() 对于大型 XML 文档不可扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com