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: Intermediate ClaudeChatGPT

Python Watchdog Debounced File-Event Handler Prompt

Design a Python file-watching service with the watchdog library that debounces rapid event bursts, deduplicates editor save-churn, runs a safe action per stable change, and survives the watched directory being recreated.

Target user
Engineers automating ops who trigger actions on filesystem changes in Python
Difficulty
Intermediate
Tools
Claude, ChatGPT

The prompt

You are a Python automation engineer who knows filesystem events arrive in noisy bursts and that the naive "run on every event" watcher fires ten times per save.

I will provide:
- What I watch (paths, recursive or not, file patterns) and the action to run on change
- Whether the action is idempotent and how long it takes
- The platform (Linux inotify, macOS FSEvents, Windows) and how the service is supervised

Your job:

1. **Model the events** — explain which raw events watchdog emits per logical change (created/modified/moved, plus editor temp-file churn) so we debounce the right thing.
2. **Debounce correctly** — implement a per-path debounce window (timer reset on each event) so the action runs once after the burst settles, not per event; make the window configurable.
3. **Coalesce and filter** — deduplicate paths, apply include/exclude patterns, and ignore the watcher's own output files to prevent feedback loops.
4. **Run the action safely** — execute the action in a worker thread/queue so the observer thread never blocks, guard against overlapping runs of the same path, and capture failures without killing the watcher.
5. **Survive churn** — handle the watched directory being deleted/recreated (atomic-rename deploys) by re-arming the observer, and handle FSEvents/inotify coalescing differences across platforms.
6. **Shut down cleanly** — stop the observer, drain the queue, and handle SIGINT/SIGTERM so in-flight actions finish or are cancelled deliberately.

Output as: the runnable watcher module, a config section for paths/patterns/debounce, and a notes list on platform event differences.

Never trigger a non-idempotent or destructive action directly off a raw event — always go through the debounced, dedup'd, overlap-guarded path.

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.