AW Dev Rethought

🕵️ Debugging is like being the detective in a crime movie where you are also the murderer - Filipe Fortes

🧩 Python Automation Recipes – 🔒 Lock File Mechanism


Description:

📌 Introduction

Scheduled automations are designed to run repeatedly, but what happens if one execution is still running when the next one starts?

Without protection, multiple instances of the same script may execute simultaneously, leading to duplicate processing, file corruption, conflicting updates, or multiple emails being sent.

This automation recipe shows how to implement a Lock File Mechanism to ensure only one instance of an automation runs at a time.


🔎 Explanation

  • Before starting, the script checks whether a lock file already exists.
  • If a lock file is found, another instance is already running, so the script exits safely.
  • If no lock file exists:
    • a lock file is created,
    • the automation runs,
    • the lock file is removed after completion.
  • A try...finally block guarantees the lock file is cleaned up even if an error occurs.

This pattern is commonly used in:

  • cron jobs
  • ETL pipelines
  • scheduled backups
  • report generators
  • production automation systems

✅ Key Takeaways

  • 🔒 Prevent multiple instances of the same automation.
  • ⚙️ Avoid duplicate processing and race conditions.
  • 🚀 Essential for reliable scheduled workflows.

Code Snippet:

# Import Path for file handling
from pathlib import Path

# Import time to simulate work
import time

# --- Step 1: Configure lock file ---
LOCK_FILE = Path("automation.lock")

# --- Step 2: Check if another instance is already running ---
if LOCK_FILE.exists():
    print("Another automation instance is already running.")
    print("Exiting safely...")
    raise SystemExit()

# --- Step 3: Create lock file ---
LOCK_FILE.write_text("Automation is running")

print("Lock acquired.")
print("Starting automation...\n")

try:

    # -----------------------------
    # Simulated automation workflow
    # -----------------------------
    for step in range(1, 6):

        print(f"Running task {step}...")
        time.sleep(1)

    print("\nAutomation completed successfully.")

finally:

    # --- Step 4: Always remove lock file ---
    if LOCK_FILE.exists():
        LOCK_FILE.unlink()

    print("Lock released.")

Link copied!

Comments

Add Your Comment

Comment Added!