目前我正在使用Python的多处理功能。尽管这对于高达200万条记录的文本文件来说效果良好,但对于具有800万条记录且";无法访问锁">
此外,处理包含200万条记录的文件大约需要30分钟,而处理大文件大约需要一个小时左右才能失败。
我在做这个:
def try_multiple_operations(item):
aab_type = item[15:17]
aab_amount = item[35:46]
aab_name = item[82:100]
aab_reference = item[64:82]
if aab_type not in '99' or 'Z5':
aab_record = f'{aab_name} {aab_amount} {aab_reference}'
else:
aab_record = 'ignore'
return aab_record
调用__main__
:中的try_multiple_operations
if __name__ == '__main__':
//some other code
executor = concurrent.futures.ProcessPoolExecutor(10)
futures = [executor.submit(try_multiple_operations, item) for item in aab ]
concurrent.futures.wait(futures)
aab_list = [x.result() for x in futures]
aab_list.sort()
//some other code for further processing
我也用过熊猫/数据帧。我可以用它做一些处理。然而,我希望在处理后能够保留文件的原始格式,因为数据帧以ndarray格式或acsv格式返回数据,所以这些数据帧会变得有点棘手。
我想知道是否有一种更快的方法可以做到这一点,也许可以使用其他编程语言。
根据@wwii的建议,更新了代码以摆脱多处理。这使得代码变得更快。
def try_multiple_operations(items):
data_aab = []
value = ['Z5','RA','Z4','99', 99]
for item in items:
aab_type = item[15:17]
aab_amount = item[35:46]
aab_name = item[82:100]
aab_reference = item[64:82]
if aab_type not in value:
# aab_record = f'{aab_name} {aab_amount} {aab_reference}'
data_aab.append(f'{aab_name} {aab_amount} {aab_reference}')
return data_aab
称为:
if __name__ == '__main__':
#some code
aab_list = try_multiple_operations(aab)
#some extra code
这虽然让我有点惊讶,但比多处理快得多。