I have a long-running process which writes a lot of stuff in a file. The result should be everything or nothing, so I\'m writing to a temporary file and rename it to the rea
You could use the lockfile module to lock the file while you are writing to it. Any subsequent attempt to lock it will block until the lock from the previous process/thread has been released.
from lockfile import FileLock
with FileLock(filename):
#open your file here....
This way, you circumvent your concurrency issues and do not have to clean up any leftover file if an exception occurs.