I am using python multiprocessing functionality to parallelize the processing of a large raster dataset. It all seems to work fine. Once processing is complete I need to automatically delete all the files that have been created by all the parallel processes. However, this is failing with the following error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'xxxxxxxx.tif'
This is the code I'm using:
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
When calling delete_func() it starts deleting all the files created during the multiprocessing (hundreds), but at some point it throws the error above mentioned. It seems some process(es) is/are still holding on to one or some of the files. How can I make sure all processes are closed and all files are "free" to be deleted?