【发布时间】:2021-07-14 06:03:02
【问题描述】:
我有一个包含 50 万行的文件 (input.txt),我想用我的 encrypt 函数加密这些行,并将它们保存到一个名为 output.txt 的文件中。例如input.txt 是
aab
abb
abc
那我想让我的output.txt成为
001
011
012
简单的for循环版本
我有一个有效的for 循环,但是加密所有行需要将近 9 个小时:
encryption_map = {}
encryption_map['a']=0
encryption_map['b']=1
encryption_map['c']=2
def encrypt(input_str):
output_int = ''
for i in input_str:
for ch in i.split('\n')[0]: # remove line break symbol \n
output_int += str(encryption_map[ch])
return output_int
text_path = 'input.txt'
with open(text_path, 'r') as input_file:
lines = input_file.readlines()
with open('output.txt', 'w') as output_file:
for l in lines:
output_int = encrypt(l)
output_file.write(output_int + '\n')
apply_async版本
由于我想保持相同的顺序,在output.txt,看来我必须使用apply_async。那么我的代码就变成了:
import multiprocessing as mp
encryption_map = {}
encryption_map['a']=0
encryption_map['b']=1
encryption_map['c']=2
def encrypt(input_str):
output_int = ''
for i in input_str:
for ch in i.split('\n')[0]: # remove line break symbol \n
output_int += str(encryption_map[ch])
return output_int
def write_result(output):
output_file.write(ipa_output + '\n')
# output_file.flush() # This line is suggested by another stack question
pool = mp.Pool(20)
text_path = 'input.txt'
with open(text_path, 'r') as input_file:
lines = input_file.readlines()
with open('output.txt', 'w') as output_file:
for l in lines:
pool.apply_async(encrypt, args=l, callback=write_result)
pool.close()
pool.join()
它运行得更快,但是 output.txt 始终为空。我的代码有什么问题?我找到了一个post,写出文件也有困难,他们建议我们把f.flush()放在write函数里面,但是也不管用。
【问题讨论】:
标签: python python-3.x multithreading multiprocessing