我有几个正在解析的大型xml文件(提取一些数据子集并写入文件),但是每个文件都有很多文件和大量记录,所以我尝试并行化。在
首先,我有一个生成器从文件中提取记录(这很好):def reader(files):
n=0
for fname in files:
chunk = ''
with gzip.open(fname, "r") as f:
for line in f:
line = line.strip()
if ''):
chunk += line
continue
if line == '':
chunk += line
n += 1
yield chunk
chunk = ''
一个过程函数(这里的细节与此无关,但也可以很好地工作):
^{pr2}$
当然,这种天真的、非并行的方法很简单:records = reader(files)
with open(output_filepath,'w') as fout:
for record in records:
result = process(record)
fout.write(result+'\n')
但现在我想把它并行化。我首先考虑使用一个简单的基于映射的方法,每个进程处理其中一个文件,但是文件的大小完全不同(有些文件非常大),所以我认为这是并行化的