"""Offline advisory-lock example, using only disposable local files.""" import fcntl,json,os,platform,tempfile from pathlib import Path def try_lock(file): try: fcntl.flock(file.fileno(),fcntl.LOCK_EX|fcntl.LOCK_NB) return True except BlockingIOError:return False def run(): with tempfile.TemporaryDirectory(prefix='reader-flock-lab-') as directory: path=Path(directory)/'job.lock' with path.open('w+') as first: assert try_lock(first) with path.open('r+') as same: before=try_lock(same) assert before is False original_inode=os.fstat(first.fileno()).st_ino path.unlink() with path.open('w+') as replacement: replacement_inode=os.fstat(replacement.fileno()).st_ino after=try_lock(replacement) assert original_inode!=replacement_inode and after is True result={'kind':'disposable_local_flock_example','performed_date_kst':'2026-10-24','environment':{'python':platform.python_version(),'system':platform.system()},'original_descriptor_still_open':not first.closed,'same_existing_path_second_lock_acquired':before,'recreated_path_points_to_different_inode':original_inode!=replacement_inode,'recreated_path_second_lock_acquired':after,'interpretation':'Locks target open file descriptions, not a permanent pathname identity. Deleting a lock path can produce a different target while an old descriptor remains open.','limits':'Same-process independently opened descriptions, local temporary filesystem; no cron, systemd, distributed or production jobs were exercised.'} fcntl.flock(replacement.fileno(),fcntl.LOCK_UN) fcntl.flock(first.fileno(),fcntl.LOCK_UN) return result if __name__=='__main__':print(json.dumps(run(),ensure_ascii=False,indent=2))