Course contentsShow
← Python
Lesson 395 of 1,0606. Error Handling and File I/OFree lesson

Managing Data Integrity with File Locking

Prevent data corruption during concurrent write operations by implementing file-level locking mechanisms to ensure exclusive access to shared storage files.

In this lesson, you will learn how to prevent data corruption by using file locking to ensure that only one process can write to a file at any given time.

The Problem: The "Race Condition"

When multiple programs or scripts attempt to modify the same file simultaneously, you risk "race conditions." Imagine two processes reading the same file, making changes, and then writing back. If Process A finishes writing its data just as Process B overwrites the file, Process A’s work is lost, or worse, the file becomes garbled. This happens because writing to a file is not an atomic operation; it takes time, and the system might switch tasks in the middle of a write.

Implementing Exclusive Access

To solve this, we use file locking. A lock acts like a "Do Not Disturb" sign on a door. When a process wants to write, it requests an exclusive lock. If the lock is granted, the operating system prevents any other process from obtaining a lock on that same file until the first process is finished and releases it.

While Python’s standard library doesn't have a cross-platform lock primitive, we use the fcntl module on Unix-based systems to request a lock on a file descriptor:

import fcntl

# Open a file for writing
with open("shared_data.txt", "a") as f:
    # Request an exclusive lock (LOCK_EX)
    # This will block until the lock is acquired
    fcntl.flock(f.fileno(), fcntl.LOCK_EX)
    
    try:
        f.write("Important synchronized data\n")
        f.flush() # Ensure data is written before unlocking
    finally:
        # Always release the lock
        fcntl.flock(f.fileno(), fcntl.LOCK_UN)

Why This Matters

By using fcntl.LOCK_EX, you ensure that the write operation is "exclusive." Even if another script tries to open the same file, it will pause at the flock line until your script finishes its work and reaches the LOCK_UN (unlock) command. This guarantees that your data remains intact and consistent, regardless of how many other processes are trying to access the file at the same time.

Key Takeaway: File locking acts as a synchronization barrier, forcing concurrent processes to queue up, thereby preventing data collisions and ensuring that your file writes are protected from partial or conflicting updates.