code: def flock(path):
      
      with open(path, "w+") as lf:
          try:
              fcntl.flock(lf, fcntl.LOCK_EX | fcntl.LOCK_NB)
              acquired = True
              yield acquired
  
          except OSError:
              acquired = False
              yield acquired
  
          finally:
              if acquired:
                  fcntl.flock(lf, fcntl.LOCK_UN)
docs: Attempt to acquire a POSIX file lock.