Skip to content

Instantly share code, notes, and snippets.

@neerajkanhere
Forked from lonetwin/locked_open.py
Created July 13, 2021 20:17
Show Gist options
  • Save neerajkanhere/b81503f58c36eca8b0b26c278c180d7f to your computer and use it in GitHub Desktop.
Save neerajkanhere/b81503f58c36eca8b0b26c278c180d7f to your computer and use it in GitHub Desktop.
Simple python file locking context manager example on linux using python stdlib's `fcntl.flock`
import logging
import fcntl
from contextlib import contextmanager
@contextmanager
def locked_open(filename, mode='r'):
"""locked_open(filename, mode='r') -> <open file object>
Context manager that on entry opens the path `filename`, using `mode`
(default: `r`), and applies an advisory write lock on the file which
is released when leaving the context. Yields the open file object for
use within the context.
Note: advisory locking implies that all calls to open the file using
this same api will block for both read and write until the lock is
acquired. Locking this way will not prevent the file from access using
any other api/method.
"""
with open(filename, mode) as fd:
fcntl.flock(fd, fcntl.LOCK_EX)
logging.debug('acquired lock on %s', filename)
yield fd
# raw_input('press enter to release lock ')
logging.debug('releasing lock on %s', filename)
fcntl.flock(fd, fcntl.LOCK_UN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment