本文整理匯總了Python中msvcrt.LK_LOCK屬性的典型用法代碼示例。如果您正苦於以下問題:Python msvcrt.LK_LOCK屬性的具體用法?Python msvcrt.LK_LOCK怎麽用?Python msvcrt.LK_LOCK使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類msvcrt
的用法示例。
在下文中一共展示了msvcrt.LK_LOCK屬性的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __enter__
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def __enter__(self):
self._fo = open(self._filename, self._mode)
# Regardless of opening mode we always seek to the beginning of file.
# This simplifies code working with LockedFile and also ensures that
# we lock (and unlock below) always the same region in file on win32.
self._fo.seek(0)
try:
if sys.platform == 'win32':
# We are locking here fixed location in file to use it as
# an exclusive lock on entire file.
msvcrt.locking(self._fo.fileno(), msvcrt.LK_LOCK, 1)
else:
fcntl.flock(self._fo.fileno(), fcntl.LOCK_EX)
except IOError:
self._fo.close()
raise
return self._fo
示例2: _lock_file
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def _lock_file(file, block):
# Lock just the first byte of the file
retry = True
while retry:
retry = False
try:
msvcrt.locking(
file.fileno(), msvcrt.LK_LOCK if block else msvcrt.LK_NBLCK, 1
)
except OSError as e:
if block and e.errno == 36:
# Windows says 'resource deadlock avoided', but we truly want
# a longer blocking wait: try again
retry = True
else:
raise LockError(
"Couldn't lock {0}, errno is {1}".format(file.name, e.errno)
)
示例3: lock_exclusive
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def lock_exclusive(self, length, start=0, whence=0, nb=0):
"""Locking method compatible with Posix files."""
if nb:
mode = msvcrt.LK_NBLCK
else:
mode = msvcrt.LK_LOCK
orig = self.tell()
self.seek(start, whence)
try:
msvcrt.locking(self.fileno(), mode, length)
finally:
self.seek(orig)
示例4: _lock_file
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def _lock_file(self, name, f):
if name == self.path:
size = self.size
else:
size = self.tops[name]
import msvcrt
for p in range(0, min(size, MAXLOCKRANGE), MAXLOCKSIZE):
f.seek(p)
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, min(MAXLOCKSIZE, size - p))
示例5: _lock_file
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def _lock_file(self, name, f):
import msvcrt
for p in range(0, min(self.sizes[name], MAXLOCKRANGE), MAXLOCKSIZE):
f.seek(p)
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, min(MAXLOCKSIZE, self.sizes[name] - p))
示例6: acquire
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def acquire(self):
msvcrt.locking(self.handle.fileno(), msvcrt.LK_LOCK, 1)
示例7: lock
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def lock(file_):
try:
savepos = file_.tell()
file_.seek(0)
try:
msvcrt.locking(file_.fileno(), msvcrt.LK_LOCK, 1)
except IOError as e:
raise SensorsAnalyticsFileLockException(e)
finally:
if savepos:
file_.seek(savepos)
except IOError as e:
raise SensorsAnalyticsFileLockException(e)
示例8: acquire
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def acquire(self, blocking):
if not self._lock.acquire(blocking):
return False
while True:
try:
fd = os.open(self._lock_path, OPEN_MODE)
msvcrt.locking(fd, msvcrt.LK_LOCK if blocking else msvcrt.LK_NBLCK, 1)
self._fd = fd
return True
except OSError:
if not blocking:
self._lock.release()
return False
示例9: _locked
# 需要導入模塊: import msvcrt [as 別名]
# 或者: from msvcrt import LK_LOCK [as 別名]
def _locked(
fileno: int,
blocked_cb: Callable[[], None],
) -> Generator[None, None, None]:
try:
# TODO: https://github.com/python/typeshed/pull/3607
msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region) # type: ignore
except OSError:
blocked_cb()
while True:
try:
# TODO: https://github.com/python/typeshed/pull/3607
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region) # type: ignore # noqa: E501
except OSError as e:
# Locking violation. Returned when the _LK_LOCK or _LK_RLCK
# flag is specified and the file cannot be locked after 10
# attempts.
if e.errno != errno.EDEADLOCK:
raise
else:
break
try:
yield
finally:
# From cursory testing, it seems to get unlocked when the file is
# closed so this may not be necessary.
# The documentation however states:
# "Regions should be locked only briefly and should be unlocked
# before closing a file or exiting the program."
# TODO: https://github.com/python/typeshed/pull/3607
msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region) # type: ignore