Skip to content

Instantly share code, notes, and snippets.

@zhangli2946
Last active October 24, 2016 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangli2946/f2233f5affc3e18c47c8c865c5451faa to your computer and use it in GitHub Desktop.
Save zhangli2946/f2233f5affc3e18c47c8c865c5451faa to your computer and use it in GitHub Desktop.
class SingletonLock(object):
"""docstring for SingletonLock"""
LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
_overlapped = pywintypes.OVERLAPPED()
def __init__(self, lockfile,flag):
super(SingletonLock, self).__init__()
self._lockfile_ = open(lockfile,"w")
self._hfile_ = win32file._get_osfhandle(self._lockfile_.fileno())
self._flag_ = flag
def __enter__(self):
win32file.LockFileEx(self._hfile_, self._flag_, 0, long(0xffff0000),self._overlapped)
return self._lockfile_
def __exit__(self, exec_type, exec_value, exce_traceback):
try:
win32file.UnlockFileEx(self._hfile_, 0, long(0xffff0000), self._overlapped)
self._lockfile_.close()
pass
except Exception, e:
return False
else:
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment