Skip to content

Instantly share code, notes, and snippets.

@imankulov
Last active December 5, 2017 06:46
Show Gist options
  • Save imankulov/3704287 to your computer and use it in GitHub Desktop.
Save imankulov/3704287 to your computer and use it in GitHub Desktop.
This is how we do file locks (python 2.5+)
from __future__ import with_statement
import os
import time
import fcntl
import contextlib
@contextlib.contextmanager
def file_lock(filename, lock_type, timeout=None):
fd = open(filename, 'w')
fctnl_lock_type = getattr(fcntl, lock_type, None)
if fctnl_lock_type is None:
raise RuntimeError('Cannot find lock_type %s' % lock_type)
# try to acquire the lock
if timeout is not None:
for i in xrange(int(timeout * 10)):
try:
fcntl.flock(fd, fctnl_lock_type | fcntl.LOCK_NB)
break
except IOError:
pass
time.sleep(0.1)
else:
fd.close()
raise IOError('Unable to acquire lock for %s' % filename)
else:
fcntl.flock(fd, fctnl_lock_type)
# execute the main function
try:
# ... right here
yield
finally:
# release the lock by closing the file
fd.close()
@dequn
Copy link

dequn commented Dec 5, 2017

else sentence in line 22 has no responding if?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment