本文整理匯總了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