def test_non_blocking_locking_without_previous_lock(locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) # Try to lock first assert store.try_aquire_lock(path) is True assert store.have_lock(path) is True store.release_lock(path) assert store.have_lock(path) is False
def test_non_blocking_locking_while_already_locked(locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) # Now take lock with t1.store t1.lock() # And now try to get the lock (which should not be possible) assert store.try_aquire_lock(path) is False assert store.have_lock(path) is False
def lock_with_pid_file(path: Path) -> None: """ Use this after daemonizing or in foreground mode to ensure there is only one process running. """ if not store.try_aquire_lock(str(path)): raise MKGeneralException("Failed to aquire PID file lock: " "Another process is already running") # Now that we have the lock we are allowed to write our pid to the file. # The pid can then be used by the init script. with path.open("w", encoding="utf-8") as f: f.write("%d\n" % os.getpid())
def test_non_blocking_locking_while_already_locked(locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) # Now take lock with t1.store t1.do = "lock" for _dummy in range(20): if t1.store.have_lock(path): break time.sleep(0.01) assert t1.store.have_lock(path) is True # And now try to get the lock (which should not be possible) assert store.try_aquire_lock(path) is False assert t1.store.have_lock(path) is True assert store.have_lock(path) is False