Locking a file in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
File locking prevents multiple processes from stepping on the same file at the same time. In Python, the right approach depends on your operating system, because Unix and Windows use different locking APIs and most locks are advisory rather than magically enforced by the filesystem itself.
Advisory Locks Versus Mandatory Behavior
The first concept to understand is that file locks are usually advisory. That means cooperating processes must also use the locking protocol. If one process ignores it and writes directly, the OS may still allow the write.
So locking works well when:
- your own processes all respect the lock
- your application controls the read and write paths
- you want to serialize file updates safely
It is not a perfect defense against arbitrary external processes.
Unix Example With fcntl.flock
On Unix-like systems, fcntl.flock() is the common tool.
LOCK_EX means an exclusive lock. Only one cooperating process should hold it at a time. If you only need shared read access, use LOCK_SH.
To fail immediately instead of waiting, combine with LOCK_NB:
Then catch BlockingIOError if another process already holds the lock.
Windows Has A Different API
On Windows, fcntl is not available. One option is msvcrt.locking(), though many Python developers choose a cross-platform third-party library instead because the platform-specific details are awkward.
That is an important design point: if your code must run on both Unix and Windows, a portability wrapper is often worth it.
A Cross-Platform Option With filelock
The filelock package is simple and widely used. It creates a separate lock file and works across platforms.
This is often the most practical answer for application code because it avoids platform branching and keeps the locking logic explicit.
Pick The Right Locking Strategy
There are two common patterns:
- lock the target file directly
- lock a separate companion file such as
data.txt.lock
A separate lock file is easier to reason about in many scripts and services. Direct file locking is closer to the underlying resource, but it may be less portable depending on your API choices.
Timeouts And Retries
Lock acquisition can block. Decide what your application should do when the file is busy:
- wait until the lock is free
- fail fast
- retry with backoff
A simple retry loop looks like this:
This is useful in batch jobs or worker processes where short contention bursts are normal.
Locking Does Not Replace Flush And Sync
Locking controls concurrency, but it does not automatically guarantee durable writes. If durability matters, flush the file and consider os.fsync() after writing.
That distinction matters for logs, ledgers, and job state files.
Common Pitfalls
- Assuming file locks are mandatory for every process on the machine.
- Forgetting that Unix and Windows use different APIs.
- Locking the file but writing through a separate path that does not respect the lock.
- Holding the lock longer than necessary, which increases contention.
- Forgetting to flush or sync when write durability matters.
Summary
- File locking in Python is mainly about coordinating cooperating processes.
- Use
fcntl.flock()on Unix-like systems for direct file locks. - Use a library such as
filelockfor a simpler cross-platform solution. - Decide whether your application should block, fail fast, or retry when a lock is busy.
- Locking prevents concurrent access problems, but it does not automatically make writes durable.
Related reading
- Locking pattern for proper use of .NET MemoryCache
- Locking pattern for proper use of .NET MemoryCache
- Log4Net FileAppender not thread safe?
- Logical CallContext values not flowing using async Tasks
- Log exception with traceback in Python
- logger configuration to log to file and print to stdout
- Logical Time, Lamport Timestamps and Vector Clocks in distributed systems
- Loop doesn't see value changed by other thread without a print statement
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.