from pathlib import Path import time LOCK_DIR = Path("/var/run/tg-bot") LOCK_DIR.mkdir(parents=True, exist_ok=True) def lock_path(name: str) -> Path: return LOCK_DIR / f"{name}.lock" def acquire_lock(name: str) -> bool: p = lock_path(name) if p.exists(): return False p.write_text(str(time.time())) return True def release_lock(name: str): p = lock_path(name) if p.exists(): p.unlink()