Skip to content

Instantly share code, notes, and snippets.

@joshmoore
Forked from chris-allan/lock_files_in_directory.py
Last active December 14, 2015 16:58
Show Gist options
  • Save joshmoore/5118585 to your computer and use it in GitHub Desktop.
Save joshmoore/5118585 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import traceback
import tempfile
import fcntl
import sys
import os
try:
d, = sys.argv[1:]
except ValueError:
print 'Usage: %s <directory>' % sys.argv[0]
sys.exit(1)
for x in (d, "%s/.omero" % d):
path = os.path.join(x, ".omero_lock_test")
print 'Locking %s:' % path,
f = open(path, 'a+')
try:
fcntl.flock(f, fcntl.LOCK_NB|fcntl.LOCK_EX)
fcntl.flock(f, fcntl.LOCK_UN)
print 'SUCCESS'
except:
#traceback.print_exc()
print 'FAILED'
finally:
f.close()
try:
os.remove(filename)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment