我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用msvcrt.LK_NBLCK。
def _lock(fileno): """Try to lock a file. Return True on success.""" # closing the file unlocks it, so we don't need to unlock here if platform.system() == 'Windows': try: msvcrt.locking(fileno, msvcrt.LK_NBLCK, 10) return True except PermissionError: return False else: try: fcntl.lockf(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB) return True # the docs recommend catching both of these except (BlockingIOError, PermissionError): return False
def trylock(self): msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_NBLCK, 1)
def _trylock(lockfile): fileno = lockfile.fileno() msvcrt.locking(fileno, msvcrt.LK_NBLCK, 1)
def lock(self, no_wait=False): import msvcrt if no_wait: op = msvcrt.LK_NBLCK else: op = msvcrt.LK_LOCK self.fd.seek(0) msvcrt.locking(self.fd, op, 1)
def trylock(self): if self.lockfile is None or self.lockfile.closed: self.lockfile = open(self.path, 'a') try: if os.name == 'nt': msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_NBLCK, 1) else: fcntl.lockf(self.lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) return True except IOError,e: if e.errno in (errno.EACCES, errno.EAGAIN): return False raise
def _lock_file(self): # Lock just the first byte try: msvcrt.locking(self.fp.fileno(), msvcrt.LK_NBLCK, 1) except IOError: raise LockError(self.fp.name)