Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fcntl
- from contextlib import contextmanager
- @contextmanager
- def flocked(fd):
- """ Locks FD before entering the context, always releasing the lock. """
- try:
- fcntl.flock(fd, fcntl.LOCK_EX)
- yield
- finally:
- fcntl.flock(fd, fcntl.LOCK_UN)
- if __name__ == "__main__":
- with open('test') as f:
- with flocked(f):
- print f.read()
Add Comment
Please, Sign In to add comment