Several instances of my script may want to clean up a folder called logs
. I want to give access to one script at a time. If another script is in there just ignore the clean up and continue. My implementation is as follows:
lockfile = "LOCKED"
if not os.path.isfile(lockfile):
open(lockfile, "a").close()
# start clean up
os.remove(lockfile)
Is this safe? I can imagine that the if statement evaluates to true before either script has created the lock file. Could this happen? How would you implement such a lock?