Cross-platform Python locking
Coordinate access. Keep your code simple.¶
File locks for Python on Linux, macOS and Windows. Use a context manager to coordinate access between processes, or Redis locks across machines.
Start with a file lock¶
Install portalocker on Python 3.10 or later:
python -m pip install portalocker
Use the file handle inside the context:
import portalocker
with portalocker.Lock('report.txt', 'a', timeout=5) as fh:
fh.write('Report complete.\n')
Lock waits up to five seconds to acquire the file lock. Leaving the
context releases the lock and closes the file, including when the body raises
an exception. Give every participating process the same file path.
Choose the lock for your task¶
File locks
Coordinate readers and writers on a shared filesystem.
File locking guideRedis locks
Coordinate processes across machines using Redis pub/sub.
Redis guideWorkers and slots
Run one worker with a PID file, or limit concurrency with a semaphore.
Lock typesNote
Unix file locks are advisory, so every participating process must lock.
Shared file locks on Windows require the optional win32 extra.
Check Platform Behaviour for filesystem behaviour and network storage caveats.