I am trying to make file read and update operations thread safe and prevent race conditions in Python. Am I missing out something, or would this work in production?import osimport timefrom contextlib import contextmanagerimport errnocurrent_milli_time = lambda: int(round(time.time() * 1000))lock_retry_wait = 0.05 # in seconds@contextmanagerdef exclusive_file(filename, mod, lock_wait_timeout=3000):    lockfile = filename + .lock    wait_start = current_milli_time()    while True:        try:            # acquire lock            fd = os.open(lockfile, os.O_CREAT|os.O_EXCL)            break        except OSError as exception:            if exception.errno != errno.EEXIST:                raise            else:                t = current_milli_time()                if (t - wait_start) > lock_wait_timeout:                    raise                time.sleep(lock_retry_wait)    with open(filename, mod) as f:        yield f    os.close(fd)    os.unlink(lockfile)with exclusive_file(test.txt,a+) as f:    previous_value = f.read()    f.truncate()    f.write(previous_value + str(time.time()))    time.sleep(20)
Thread Safe File Operation
python;file;thread safety