本文整理汇总了Python中msvcrt.locking方法的典型用法代码示例。如果您正苦于以下问题:Python msvcrt.locking方法的具体用法?Python msvcrt.locking怎么用?Python msvcrt.locking使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msvcrt
的用法示例。
在下文中一共展示了msvcrt.locking方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __enter__
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [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: __exit__
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def __exit__(self, exc_type, exc_value, traceback):
# Flush any buffered data to disk. This is needed to prevent race
# condition which happens from the moment of releasing file lock
# till closing the file.
self._fo.flush()
try:
if sys.platform == 'win32':
self._fo.seek(0)
msvcrt.locking(self._fo.fileno(), msvcrt.LK_UNLCK, 1)
else:
fcntl.flock(self._fo.fileno(), fcntl.LOCK_UN)
finally:
self._fo.close()
return exc_value is None
示例3: __init__
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def __init__(self, lock_file, timeout = -1):
"""
"""
# The path to the lock file.
self._lock_file = lock_file
# The file descriptor for the *_lock_file* as it is returned by the
# os.open() function.
# This file lock is only NOT None, if the object currently holds the
# lock.
self._lock_file_fd = None
# The default timeout value.
self.timeout = timeout
# We use this lock primarily for the lock counter.
self._thread_lock = threading.Lock()
# The lock counter is used for implementing the nested locking
# mechanism. Whenever the lock is acquired, the counter is increased and
# the lock is only released, when this value is 0 again.
self._lock_counter = 0
return None
示例4: _release
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def _release(self):
fd = self._lock_file_fd
self._lock_file_fd = None
msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
os.close(fd)
try:
os.remove(self._lock_file)
# Probably another instance of the application
# that acquired the file lock.
except OSError:
pass
return None
# Unix locking mechanism
# ~~~~~~~~~~~~~~~~~~~~~~
示例5: __init__
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def __init__(self, lock_file, timeout=-1):
"""
"""
# The path to the lock file.
self._lock_file = lock_file
# The file descriptor for the *_lock_file* as it is returned by the
# os.open() function.
# This file lock is only NOT None, if the object currently holds the
# lock.
self._lock_file_fd = None
# The default timeout value.
self.timeout = timeout
# We use this lock primarily for the lock counter.
self._thread_lock = threading.Lock()
# The lock counter is used for implementing the nested locking
# mechanism. Whenever the lock is acquired, the counter is increased and
# the lock is only released, when this value is 0 again.
self._lock_counter = 0
return None
示例6: _release
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def _release(self):
fd = self._lock_file_fd
self._lock_file_fd = None
msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
os.close(fd)
try:
os.remove(self._lock_file)
# Probably another instance of the application
# that acquired the file lock.
except OSError:
pass
return None
# Unix locking mechanism
# ~~~~~~~~~~~~~~~~~~~~~~
示例7: __init__
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def __init__(self, lock_file, timeout = -1):
"""
"""
# The path to the lock file.
if lock_file.endswith(".lock") is False: lock_file += ".lock"
self._lock_file = lock_file
# The file descriptor for the *_lock_file* as it is returned by the
# os.open() function.
# This file lock is only NOT None, if the object currently holds the
# lock.
self._lock_file_fd = None
# The default timeout value.
self.timeout = timeout
# We use this lock primarily for the lock counter.
self._thread_lock = threading.Lock()
# The lock counter is used for implementing the nested locking
# mechanism. Whenever the lock is acquired, the counter is increased and
# the lock is only released, when this value is 0 again.
self._lock_counter = 0
return None
示例8: _acquire
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def _acquire(self):
open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
try:
fd = os.open(self._lock_file, open_mode)
except OSError:
pass
else:
try:
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
except (IOError, OSError):
try:
os.close(fd)
except:
fd.close()
else:
self._lock_file_fd = fd
return None
示例9: _release
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def _release(self):
fd = self._lock_file_fd
self._lock_file_fd = None
msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
try:
os.close(fd)
except:
fd.close()
try:
os.remove(self._lock_file)
# Probably another instance of the application
# that acquired the file lock.
except OSError:
pass
return None
# Unix locking mechanism
# ~~~~~~~~~~~~~~~~~~~~~~
示例10: _lock_file
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [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)
)
示例11: lock_files
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def lock_files(handles_dir, out_queue):
with lockutils.lock('external', 'test-', external=True):
# Open some files we can use for locking
handles = []
for n in range(50):
path = os.path.join(handles_dir, ('file-%s' % n))
handles.append(open(path, 'w'))
# Loop over all the handles and try locking the file
# without blocking, keep a count of how many files we
# were able to lock and then unlock. If the lock fails
# we get an IOError and bail out with bad exit code
count = 0
for handle in handles:
try:
lock_file(handle)
count += 1
unlock_file(handle)
except IOError:
os._exit(2)
finally:
handle.close()
return out_queue.put(count)
示例12: dump_to_file_and_close
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def dump_to_file_and_close(self):
json.dump(self.test_results, self.json_dump_file)
self.json_dump_file.close()
# Record of test runtimes. Has built-in locking.
示例13: timeout
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def timeout(self, value):
"""
"""
self._timeout = float(value)
return None
# Platform dependent locking
# --------------------------------------------
示例14: __del__
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def __del__(self):
self.release(force = True)
return None
# Windows locking mechanism
# ~~~~~~~~~~~~~~~~~~~~~~~~~
示例15: _acquire
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def _acquire(self):
open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
try:
fd = os.open(self._lock_file, open_mode)
except OSError:
pass
else:
try:
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
except (IOError, OSError):
os.close(fd)
else:
self._lock_file_fd = fd
return None