Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import fcntl
# Expected output:
# lock acquired
# failed to lock
# lock acquired
lf = '/tmp/some.lock'
h = None
def lock(path):
global h
# w is used so we don't re-use the same FD, this simulates as if separate programs are running this code
w = None
if not h:
h = open(lf, 'w')
w = h
w = open(lf, 'w')
fcntl.flock(w, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
return False
print 'lock acquired'
return True
if not lock(lf):
print 'failed to lock'
if not lock(lf):
print 'failed to lock'
if not lock(lf):
print 'failed to lock'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.