So I have a script that will get fired by incrond upon when new file events are detected in a watch dir. In the script I have this function which will read and save stuff to conf file. At the moment, I try not to allow more than one instance of the script to run to avoid race condition and end up corrupting the saved file.
And right now I'm trying to figure out how to make this function write to conf file safely when I have multiple instances of the script try to write to the same file. I read about file locking with fcntl module the other day but it's little over my head and I wasn't sure how it will work
def save_task_details(resp, filepath):
conf = ConfigParser.SafeConfigParser()
conf.read(CFG_PATH)
filehash = get_filehash(filepath)
if not conf.has_section(filehash):
conf.add_section(filehash)
conf.set(filehash, "filename", os.path.basename(filepath))
conf.set(filehash, "id", resp.id)
# ...
with open(CFG_PATH, "wb") as configfile:
conf.write(configfile)