當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。