【发布时间】:2022-06-21 22:24:40
【问题描述】:
我正在使用 python 多处理功能来并行处理大型栅格数据集。一切似乎都很好。处理完成后,我需要自动删除所有并行进程创建的所有文件。但是,这失败并出现以下错误:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'xxxxxxxx.tif'
这是我正在使用的代码:
def my_function_1(info):
...
def my_function_2(...):
...
delete_func():
...
info = {}
info = .... #I populate a dict with the parameters I want to send to the function
pool = Pool(processes=16)
pool.map(my_function_1, info.items())
pool.close()
pool.join()
my_function_2(...) #Processing files created during the multiprocessing
delete_func() #Delete files created during the multiprocessing
当调用 delete_func() 时,它开始删除在多处理期间创建的所有文件(数百个),但在某些时候它会抛出上述错误。似乎某些进程正在/仍在保留一个或一些文件。如何确保所有进程都已关闭并且所有文件都“免费”可以删除?
【问题讨论】:
-
如果没有minimal reproducible example,真的没有办法提供帮助。您甚至没有提供完整的错误消息...问题几乎可以肯定是您未提供的功能的详细信息(我怀疑
my_function_2)。 -
另外,这不应该在 Windows 上工作,因为你没有用
pool.map保护你的调用if __name__ == "__main__":保护...这应该会创建一个多处理炸弹。 -
整个代码大约600行。 my_function_2 唯一要做的就是将在多处理过程中创建的所有 tif 文件合并到一个唯一的大 tif 文件中。合并发生后,我不再需要马赛克的所有瓷砖,只想删除它们。 “多处理炸弹”是什么意思?
标签: python multiprocessing pool