threading.Lock in kodi



Hello everyone,
I have an addon that I use for my own family and that during its execution deletes a file, if it exists, and regenerates it with another content.
The problem is that when adding this addon to the widgets of my skin, this process is executed several times in an asynchronous manner, which leads to errors because different processes want to manipulate the file.

I thought about putting a lock (threading.Lock) in the critical section, but since I don’t have access to the main process that launches the widget reload threads, this is not feasible.
I then thought about using a property created in the Home window to do this task: Before entering the critical area, it checks if this property exists, if not, it is created and the instructions on the file are executed. Then the property is deleted again, releasing the “lock”. If another process reaches this point and the Home property exists, it waits in a loop until the “lock” is “opened”.

This seems to reduce the number of errors, but not completely. Is there a better method to do this?