gpt4 book ai didi

python - 在 Twistd 中无阻塞地压缩文件。

转载 作者:行者123 更新时间:2023-12-01 09:24:12 26 4
gpt4 key购买 nike

有没有一种方法可以在扭曲中不阻塞地压缩文件?

import zipfile
from twisted.internet import defer
from twisted.internet import reactor

def zip_files(file_list, path, output_Zip):
zip_handle = zipfile.ZipFile(output_zip, mode='w', allowZip64=True)
try:
for i in file_list:
zip_handle.write(i)
zip_handle.close()
return True
except Exception as e:
return False
def print_zip(res):
print res
return res


file_list = ['path_to_file1','path_to_file2']
output_path = 'full_path_to_output_zip'
d = defer.Deferred()
d.addCallback(lambda _: zip_files(file_list, output_path)
d.addCallback(print_zip)
zip_result = d
reactor.run()

到目前为止我已经有了这个。虽然它确实有效,但触发压缩过程会导致twisted 阻塞并等待初始“压缩作业”完成。我宁愿它终止现有的“压缩作业”并开始新的作业。

最佳答案

也许像这样,使用 deferToThreadDeferredList 来不阻止写入 zip 文件:

import zipfile
import logging
from twisted.internet import threads, defer
from twisted.internet import reactor

log = logging.getLogger()
log.addHandler(logging.StreamHandler())
log.setLevel(logging.INFO)


def zip_file(input_path, output_path):
with zipfile.ZipFile(output_path, mode='w', allowZip64=True) as zip_handle:
zip_handle.write(input_path)


def log_failure(err):
log.exception("error: %s", err)


def zip_file_and_catch_error(input_path, output_path):
d = threads.deferToThread(zip_file, input_path, output_path)
d.addErrback(log_failure)
return d


def main():
input_paths = ['path_to_file1','path_to_file2']
output_paths = ['path_out1','path_out2']
assert len(input_paths) == len(output_paths)
dl = defer.DeferredList([zip_file_and_catch_error(input_path, output_path)
for input_path, output_path in zip(input_paths, output_paths)])
dl.addCallback(lambda result: log.info("result: %s", result))
dl.addBoth(lambda _: reactor.callLater(0, reactor.stop))
reactor.run()


if __name__ == "__main__":
main()

关于python - 在 Twistd 中无阻塞地压缩文件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50569692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com