【发布时间】:2022-01-19 12:52:47
【问题描述】:
我制作了一个简单的应用程序来加密和解密文件。但是当我加载像 2gb 这样的大文件时,我的程序会使用 100% 的内存。我使用多处理和多线程。
poolSize = min(cpu_count(), len(fileList))
process_pool = Pool(poolSize)
thread_pool = ThreadPool(len(fileList))
lock = Lock()
worker = partial(encfile, process_pool, lock)
thread_pool.map(worker, fileList)
def encfile(process_pool, lock, file):
with open(file, 'rb') as original_file:
original = original_file.read()
encrypted = process_pool.apply(encryptfn, args=(key, original,))
with open (file, 'wb') as encrypted_file:
encrypted_file.write(encrypted)
【问题讨论】:
-
您可以通过指定读写中的字节数(举个例子:python file reading and writing。如果你的加密算法不支持块样式处理,你可以考虑使用像ChaCha20这样的流密码
-
我正在使用来自cryptography库的fernet
-
您的实际问题是什么?
-
@UlrichEckhardt 当我加密文件时,我的应用程序消耗了 100% 的内存
-
另一个问题有一个类似的问题,它有一个answer about fernet not being a good choice for encrypting large files。您可以查看用于以小块加密大文件的 ChaCha20 流密码。 This answer, about encrypting large files in blocks, might be a good starting point.
标签: python multithreading memory multiprocessing