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


Python lockfile.FileLock方法代碼示例

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


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

示例1: __enter__

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [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

示例2: test_other_process_locks

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def test_other_process_locks(self):
        """Test locking the state in another process locks
        """
        with tempfile.NamedTemporaryFile() as temp:
            def other_op(queue):
                """Lock the lockfile, then wait for poison pill
                """
                lockfile.FileLock(temp.name).acquire()
                while queue.empty():
                    sleep(0.01)
                lockfile.FileLock(temp.name).release()

            queue = multiprocessing.Queue()
            process = multiprocessing.Process(target=other_op,
                                              args=(queue,))
            process.start()

            while not lockfile.FileLock(temp.name).is_locked():
                sleep(0.01)
            StateHandler.STATE_LOCK_TIMEOUT = 0.001
            handler = StateHandler(temp.name, load=False)
            self.assertRaises(RuntimeError, handler.lock)
            queue.put(None)
            process.join() 
開發者ID:duerrp,項目名稱:pyexperiment,代碼行數:26,代碼來源:test_state.py

示例3: generate_or_read_from_file

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def generate_or_read_from_file(key_file='.secret_key', key_length=64):
    """Multiprocess-safe secret key file generator.

    Useful to replace the default (and thus unsafe) SECRET_KEY in settings.py
    upon first start. Save to use, i.e. when multiple Python interpreters
    serve the dashboard Django application (e.g. in a mod_wsgi + daemonized
    environment).  Also checks if file permissions are set correctly and
    throws an exception if not.
    """
    lock = lockfile.FileLock(key_file)
    with lock:
        if not os.path.exists(key_file):
            key = generate_key(key_length)
            old_umask = os.umask(0o177)  # Use '0600' file permissions
            with open(key_file, 'w') as f:
                f.write(key)
            os.umask(old_umask)
        else:
            if (os.stat(key_file).st_mode & 0o777) != 0o600:
                raise FilePermissionError("Insecure key file permissions!")
            with open(key_file, 'r') as f:
                key = f.readline()
        return key 
開發者ID:CiscoSystems,項目名稱:avos,代碼行數:25,代碼來源:secret_key.py

示例4: _lock_state_file

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def _lock_state_file(self):
        if not self.lock:
            return
        self._lockfile = LockFile(self.path)

        if (self._lockfile.is_locked() and
                (time() - getmtime(self._lockfile.lock_file)) > 10):
            self._lockfile.break_lock()

        self._lockfile.acquire() 
開發者ID:bq,項目名稱:web2board,代碼行數:12,代碼來源:app.py

示例5: __init__

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def __init__(self, timeout=2):
        """Create the lock file and lock file object and obtains a lock."""
        # Create lock file, if it does not exist
        if not os.path.isfile(DirectoryLocation.LOCK_FILE):
            if not os.path.isdir(DirectoryLocation.LOCK_FILE_DIR):
                os.mkdir(DirectoryLocation.LOCK_FILE_DIR)
            open(DirectoryLocation.LOCK_FILE, 'a').close()

        # Attempt to lock lockfile
        DaemonLock.LOCK = FileLock(DirectoryLocation.LOCK_FILE)

        # Check if lockfile object is already locked
        if DaemonLock.LOCK.is_locked():
            raise MCVirtLockException('An instance of MCVirt is already running') 
開發者ID:ITDevLtd,項目名稱:MCVirt,代碼行數:16,代碼來源:daemon_lock.py

示例6: __init__

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def __init__(self, name, maxattempts, filename, logger):
        self.name = name
        self.filename = filename
        self.maxattempts = maxattempts
        self.filelock = lockfile.FileLock(self.filename)
        self.counter = 0
        self.logger = logger 
開發者ID:openweave,項目名稱:happy,代碼行數:9,代碼來源:Driver.py

示例7: write_tuning_result

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def write_tuning_result(params: dict, result: dict, df_path: str):
    row = pd.DataFrame()
    for key in params['tuning_params']:
        row[key] = [params[key]]

    for key, val in result.items():
        row[key] = val

    with lockfile.FileLock(df_path):
        df_results = pd.read_csv(df_path)
        df_results = pd.concat([df_results, row], sort=False).reset_index(drop=True)
        df_results.to_csv(df_path, index=None) 
開發者ID:lyakaap,項目名稱:Landmark2019-1st-and-3rd-Place-Solution,代碼行數:14,代碼來源:utils.py

示例8: download

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def download(self):
        self._mkdir_p(self.local_directory)
        with FileLock(self.lock_location):
            if not self._check_binary_present():
                self._download_binary() 
開發者ID:botify-labs,項目名稱:simpleflow,代碼行數:7,代碼來源:download.py

示例9: _download_binary

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [as 別名]
def _download_binary(self):
        logger.info("Downloading binary: {} -> {}".format(self.remote_location, self.local_location))
        bucket, path = self.remote_location.replace("s3://", "", 1).split("/", 1)
        # with FileLock(dest):
        pull(bucket, path, self.local_location)
        os.chmod(self.local_location, 0o755)


# convenience helpers 
開發者ID:botify-labs,項目名稱:simpleflow,代碼行數:11,代碼來源:download.py

示例10: IncRunID

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [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

示例11: lock

# 需要導入模塊: import lockfile [as 別名]
# 或者: from lockfile import FileLock [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


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