Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
@claudep
Copy link

claudep commented May 28, 2021

Typos: contexlib/contexmanager => contextlib/contextmanager

Nice gist!

@lonetwin
Copy link
Author

lonetwin commented May 31, 2021

Typos: contexlib/contexmanager => contextlib/contextmanager

Nice gist!

cheers, thanks for reporting that !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment