Guest User

Untitled

a guest
Dec 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. import fcntl
  2. from contextlib import contextmanager
  3.  
  4. @contextmanager
  5. def flocked(fd):
  6. """ Locks FD before entering the context, always releasing the lock. """
  7. try:
  8. fcntl.flock(fd, fcntl.LOCK_EX)
  9. yield
  10. finally:
  11. fcntl.flock(fd, fcntl.LOCK_UN)
  12.  
  13. if __name__ == "__main__":
  14. with open('test') as f:
  15. with flocked(f):
  16. print f.read()
Add Comment
Please, Sign In to add comment