当前位置: 首页>>代码示例>>Python>>正文


Python msvcrt.locking方法代码示例

本文整理汇总了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 
开发者ID:google,项目名称:gtest-parallel,代码行数:22,代码来源:gtest_parallel.py

示例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 
开发者ID:google,项目名称:gtest-parallel,代码行数:18,代码来源:gtest_parallel.py

示例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 
开发者ID:a4k-openproject,项目名称:a4kScrapers,代码行数:25,代码来源:filelock.py

示例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
# ~~~~~~~~~~~~~~~~~~~~~~ 
开发者ID:a4k-openproject,项目名称:a4kScrapers,代码行数:18,代码来源:filelock.py

示例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 
开发者ID:team-ocean,项目名称:veros,代码行数:25,代码来源:filelock.py

示例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
# ~~~~~~~~~~~~~~~~~~~~~~ 
开发者ID:vacancy,项目名称:Jacinle,代码行数:19,代码来源:filelock.py

示例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 
开发者ID:tvaddonsco,项目名称:plugin.git.browser,代码行数:26,代码来源:filelock.py

示例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 
开发者ID:tvaddonsco,项目名称:plugin.git.browser,代码行数:20,代码来源:filelock.py

示例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
# ~~~~~~~~~~~~~~~~~~~~~~ 
开发者ID:tvaddonsco,项目名称:plugin.git.browser,代码行数:20,代码来源:filelock.py

示例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)
                        ) 
开发者ID:mideind,项目名称:ReynirPackage,代码行数:20,代码来源:glock.py

示例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) 
开发者ID:openstack,项目名称:oslo.concurrency,代码行数:25,代码来源:test_lockutils.py

示例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. 
开发者ID:google,项目名称:gtest-parallel,代码行数:8,代码来源:gtest_parallel.py

示例13: timeout

# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def timeout(self, value):
        """
        """
        self._timeout = float(value)
        return None

    # Platform dependent locking
    # -------------------------------------------- 
开发者ID:a4k-openproject,项目名称:a4kScrapers,代码行数:10,代码来源:filelock.py

示例14: __del__

# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import locking [as 别名]
def __del__(self):
        self.release(force = True)
        return None


# Windows locking mechanism
# ~~~~~~~~~~~~~~~~~~~~~~~~~ 
开发者ID:a4k-openproject,项目名称:a4kScrapers,代码行数:9,代码来源:filelock.py

示例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 
开发者ID:a4k-openproject,项目名称:a4kScrapers,代码行数:17,代码来源:filelock.py


注:本文中的msvcrt.locking方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。