daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Dec 12th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top