Python fcntl Advisory File Locking Prompt
Serialize access to a shared resource across processes using fcntl advisory locks (flock/lockf) so two script instances never run the critical section concurrently
- Target user
- Python automation engineers preventing overlapping cron jobs or concurrent writers to a shared file
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python systems engineer who specializes in POSIX advisory file locking via the `fcntl` module. I will provide: - What I am protecting (a single-instance script, a shared state file, a job queue) - Whether I need a blocking wait or an immediate "already locked, exit now" behavior - The filesystem type, since locking semantics differ on NFS and tmpfs Your job: 1. **Pick the API** — compare `fcntl.flock` (whole-file, simpler) vs. `fcntl.lockf`/`fcntl.fcntl` (byte-range, more portable to System V) and recommend one for my case. 2. **Acquire correctly** — open a dedicated lockfile, then `flock(fd, LOCK_EX | LOCK_NB)`, catching `BlockingIOError` for the non-blocking "already running" path. 3. **Wrap in a context manager** — provide a `with file_lock(path):` that acquires on enter and releases on exit, even on exception. 4. **Release reliably** — explain that the lock is tied to the open fd and is dropped on close/exit, and why you must NOT close the fd early. 5. **Warn about advisory semantics** — clarify these locks are advisory (only cooperating processes honor them) and do not stop a non-locking writer. 6. **Flag NFS/threads caveats** — note flock is unreliable over older NFS and that fcntl locks are per-process (not per-thread), so threads in one process share the lock. Output as: one Python module with the `file_lock` context manager and both a blocking and non-blocking usage example. State explicitly that these locks protect only against other processes that ALSO take the lock — never present them as mandatory locks.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Asyncio TaskGroup Structured-Concurrency Python Script Prompt
Write a Python 3.11+ automation script that fans work out with asyncio.TaskGroup so failures cancel siblings cleanly, errors aggregate into an ExceptionGroup, and no task is ever silently orphaned.
-
Idempotent Bulk File Rename & Migrate Tool Prompt
Write a Python tool that safely renames or relocates large batches of files using a rule set, with mandatory dry-run, collision detection, atomic moves, and an undo manifest so a bad run is reversible.
-
Python asyncio.create_subprocess_exec Fan-Out Prompt
Run many external commands concurrently under asyncio with bounded concurrency, captured output, and per-command timeouts.
-
Python concurrent.futures ThreadPool vs ProcessPool Selector Prompt
Decide between ThreadPoolExecutor and ProcessPoolExecutor and wire up correct exception handling and chunking.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.