Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- import errno
- import fcntl
- import sys
- FILE = './LOCK_ME.txt'
- print 'Python lockf()!'
- # Open a file
- print 'Opening file {}'.format(FILE)
- f = open(FILE, 'a+', 0)
- # Lock fun
- print 'Acquring lock on {}...'.format(FILE)
- try:
- # LOCK_EX to acquire an exclusive lock, no one else gets in
- # LOCK_NB to not block but raise exception if already locked
- fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
- except IOError as e:
- if e.errno not in (errno.EACCES, errno.EAGAIN):
- raise
- else:
- print 'Already locked!'
- sys.exit(1)
- print 'Locked {}'.format(FILE)
- _ = raw_input('Press enter to unlock and exit...')
- fcntl.lockf(f, fcntl.LOCK_UN)
- f.close()
- print 'So long!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement