當前位置: 首頁>>代碼示例>>Python>>正文


Python lockfile.LockTimeout方法代碼示例

本文整理匯總了Python中lockfile.LockTimeout方法的典型用法代碼示例。如果您正苦於以下問題:Python lockfile.LockTimeout方法的具體用法?Python lockfile.LockTimeout怎麽用?Python lockfile.LockTimeout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在lockfile的用法示例。


在下文中一共展示了lockfile.LockTimeout方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: lock

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import LockTimeout [as 別名]
def lock(self):
        wait_time = 0.1
        wait_attempts = 0

        while True:
            try:
                if self.filelock.i_am_locking():
                    self.counter += 1
                    break
                self.filelock.acquire(timeout=wait_time)
                break
            except lockfile.LockTimeout:
                pass
            wait_attempts += 1
            if wait_attempts % self.maxattempts == 0:
                emsg = "Waiting for %s for over %d seconds. Break and Kill itself." % (self.name, wait_time * wait_attempts)
                self.logger.error("[localhost] Happy: %s" % (emsg))
                raise HappyException(emsg)

            if wait_attempts % 10 == 0:
                emsg = "Waiting for %s for over %.2f seconds." % (self.name, wait_time * wait_attempts)
                self.logger.warning("[localhost] Happy: %s" % (emsg)) 
開發者ID:openweave,項目名稱:happy,代碼行數:24,代碼來源:Driver.py

示例2: __enter__

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import LockTimeout [as 別名]
def __enter__(self):
        if self.options.splay > 0:
            splay = randint(0, self.options.splay)
            self.logger.debug('Sleeping for %d seconds (splay=%d)' %
                              (splay, self.options.splay))
            time.sleep(splay)
        self.start_time = DT.datetime.today()
        if not self.options.nolock:
            self.logger.debug('Attempting to acquire lock %s (timeout %s)',
                              self.options.lockfile,
                              self.options.locktimeout)
            self.lock = FileLock(self.options.lockfile)
            try:
                self.lock.acquire(timeout=self.options.locktimeout)
            except LockFailed as e:
                self.logger.error("Lock could not be acquired.")
                self.logger.error(str(e))
                sys.exit(1)
            except LockTimeout as e:
                msg = "Lock could not be acquired. Timeout exceeded."
                self.logger.error(msg)
                sys.exit(1) 
開發者ID:jjneely,項目名稱:whisper-backup,代碼行數:24,代碼來源:pycronscript.py

示例3: test_run_pack_lock_is_already_acquired

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import LockTimeout [as 別名]
def test_run_pack_lock_is_already_acquired(self):
        action = self.get_action_instance()
        temp_dir = hashlib.md5(PACK_INDEX['test']['repo_url'].encode()).hexdigest()

        original_acquire = LockFile.acquire

        def mock_acquire(self, timeout=None):
            original_acquire(self, timeout=0.1)

        LockFile.acquire = mock_acquire

        try:
            lock_file = LockFile('/tmp/%s' % (temp_dir))

            # Acquire a lock (file) so acquire inside download will fail
            with open(lock_file.lock_file, 'w') as fp:
                fp.write('')

            expected_msg = 'Timeout waiting to acquire lock for'
            self.assertRaisesRegexp(LockTimeout, expected_msg, action.run, packs=['test'],
                                    abs_repo_base=self.repo_base)
        finally:
            os.unlink(lock_file.lock_file)
            LockFile.acquire = original_acquire 
開發者ID:StackStorm,項目名稱:st2,代碼行數:26,代碼來源:test_action_download.py

示例4: IncRunID

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import LockTimeout [as 別名]
def IncRunID(project_name, db_dir):
    """Increment the RunID and append new value with project name to the file"""
    database_file = db_dir + '/runID_database.txt'

    # lock the file
    lock = lf.FileLock(database_file)
    while not lock.i_am_locking():
        try:
            # wait up to 10 seconds
            lock.acquire(timeout=5)
        except lf.LockTimeout:
            raise Exception(
                'ERROR: Timed out waiting for file lock at ' + lock.path)

    # get the last run_id from the db file
    rundb = open(database_file, 'r')
    for line in rundb:
        (old_id, old_project) = line.split()

    rundb.close()
    global run_id
    run_id = int(old_id) + 1

    # write the incremented run_id with project name to the db file
    with open(database_file, 'a') as rundb:
        rundb.write(str(run_id) + '\t' + project_name + '\n')

    rundb.close()
    lock.release()

    return 
開發者ID:pughlab,項目名稱:bamgineer,代碼行數:33,代碼來源:runIDHelpers.py

示例5: lock

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import LockTimeout [as 別名]
def lock(self):
        """Lock the state file
        """
        self.state_lock = lockfile.FileLock(self.filename)
        try:
            self.state_lock.acquire(timeout=self.STATE_LOCK_TIMEOUT)
        except lockfile.LockTimeout:
            raise RuntimeError("Cannot acquire lock on state file ('%s'), "
                               "check if another process is using it"
                               % self.filename) 
開發者ID:duerrp,項目名稱:pyexperiment,代碼行數:12,代碼來源:State.py

示例6: download_file

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import LockTimeout [as 別名]
def download_file(
        url,
        local_filepath,
        chunk_size=1024*512,
        lock_timeout=10,
        http_timeout=None,
        session=None
):
    # pylint: disable=too-many-arguments
    """
    A helper function which can download a file from a specified ``url`` to a
    local file ``local_filepath`` in chunks and using a file lock to prevent
    a concurrent download of the same file.
    """
    # Avoid unnecessary dependencies when the function is not used.
    import lockfile
    import requests

    log.debug("Checking file existance in '%s'", local_filepath)
    lock = lockfile.LockFile(local_filepath)
    try:
        lock.acquire(timeout=lock_timeout)
    except lockfile.LockTimeout:
        log.info(
            "File '%s' is locked. Probably another instance is still downloading it.",
            local_filepath
        )
        raise
    try:
        if not os.path.exists(local_filepath):
            log.info("Downloading a file from '%s' to '%s'", url, local_filepath)
            if session is None:
                session = requests
            response = session.get(url, stream=True, timeout=http_timeout)
            if response.status_code != 200:
                log.error("Download '%s' is failed: %s", url, response)
                response.raise_for_status()
            with open(local_filepath, 'wb') as local_file:
                for chunk in response.iter_content(chunk_size=chunk_size):
                    # filter out keep-alive new chunks
                    if chunk:
                        local_file.write(chunk)
        log.debug("File '%s' has been downloaded", local_filepath)
        return local_filepath
    finally:
        lock.release() 
開發者ID:frol,項目名稱:flask-restplus-server-example,代碼行數:48,代碼來源:utils.py


注:本文中的lockfile.LockTimeout方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。