Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Advanced ClaudeChatGPT

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

More Bash & Python Automation prompts & error guides

Browse every Bash & Python Automation prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.