Last active
October 24, 2016 12:41
-
-
Save zhangli2946/f2233f5affc3e18c47c8c865c5451faa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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