为了解析一个较大的文件,我需要循环连续写入大量的parquet文件。然而,每次迭代时这个任务消耗的内存都会增加,而我希望它保持不变(因为在内存中不应该添加任何东西)。这使得扩展变得棘手。
我已经添加了一个最小可重现示例,它创建了 10,000 个parquet,并对其进行循环追加。
import resource
import random
import string
import pyarrow as pa
import pyarrow.parquet as pq
import pandas as pd
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
schema = pa.schema([
pa.field('test', pa.string()),
])
resource.setrlimit(resource.RLIMIT_NOFILE, (1000000, 1000000))
number_files = 10000
number_rows_increment = 1000
number_iterations = 100
writers = [pq.ParquetWriter('test_'+id_generator()+'.parquet', schema) for i in range(number_files)]
for i in range(number_iterations):
for writer in writers:
table_to_write = pa.Table.from_pandas(
pd.DataFrame({'test': [id_generator() for i in range(number_rows_increment)]}),
preserve_index=False,
schema = schema,
nthreads = 1)
table_to_write = table_to_write.replace_schema_metadata(None)
writer.write_table(table_to_write)
print(i)
for writer in writers:
writer.close()
有人知道是什么原因导致这个漏洞并如何预防吗?
pandas>=0.23
。Pandas存在泄漏问题,也会影响到pyarrow
。 - Uwe L. Kornpyarrow==0.15.0
对我很有帮助。 - Dima Fomin