8000 Avoid 1s delay on consequent lock in windows by awaizman1 · Pull Request #66 · tox-dev/filelock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Avoid 1s delay on consequent lock in windows #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

try:
import msvcrt
from ctypes import windll, wintypes, WinError
except ImportError:
msvcrt = None

Expand Down Expand Up @@ -335,12 +336,63 @@ def __del__(self):
# Windows locking mechanism
# ~~~~~~~~~~~~~~~~~~~~~~~~~

def _get_winapi_lock_func():

# BOOL LockFile(
# HANDLE hFile,
# DWORD dwFileOffsetLow,
# DWORD dwFileOffsetHigh,
# DWORD nNumberOfBytesToUnlockLow,
# DWORD nNumberOfBytesToUnlockHigh
# );

kernel32 = windll.kernel32
def errcheck(res, func, args):
if not res:
raise WinError()

return res

win_lock_file = kernel32.LockFile
win_lock_file.argtypes = (wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD)
win_lock_file.restype = wintypes.BOOL
win_lock_file.errcheck = errcheck

return win_lock_file

def _get_winapi_unlock_func():

# BOOL UnlockFile(
# HANDLE hFile,
# DWORD dwFileOffsetLow,
# DWORD dwFileOffsetHigh,
# DWORD nNumberOfBytesToUnlockLow,
# DWORD nNumberOfBytesToUnlockHigh
# );

kernel32 = windll.kernel32
def errcheck(res, func, args):
if not res:
raise WinError()

return res

win_unlock_file = kernel32.UnlockFile
win_unlock_file.argtypes = (wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD)
win_unlock_file.restype = wintypes.BOOL
win_unlock_file.errcheck = errcheck

return win_unlock_file

class WindowsFileLock(BaseFileLock):
"""
Uses the :func:`msvcrt.locking` function to hard lock the lock file on
Uses the :func:`LockFile` winapi function to hard lock the lock file on
windows systems.
"""

_WINAPI_LOCK_FUNC = _get_winapi_lock_func() if msvcrt else None
_WINAPI_UNLOCK_FUNC = _get_winapi_lock_func() if msvcrt else None

def _acquire(self):
open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC

Expand All @@ -350,7 +402,7 @@ def _acquire(self):
pass
else:
try:
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
self._WINAPI_LOCK_FUNC(msvcrt.get_osfhandle(fd), 0, 0, 1, 0)
except (IOError, OSError):
os.close(fd)
else:
Expand All @@ -360,7 +412,7 @@ def _acquire(self):
def _release(self):
fd = self._lock_file_fd
self._lock_file_fd = None
msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
self._WINAPI_UNLOCK_FUNC(msvcrt.get_osfhandle(fd), 0, 0, 1, 0)
os.close(fd)

try:
Expand Down
0