How to use the portalocker.Lock function in portalocker

To help you get started, we’ve selected a few portalocker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cartologic / cartoview / cartoview / app_manager / config.py View on Github external
def load(cls, apps_dir=None):
        if os.path.exists(cls.get_apps_json_path()):
            with portalocker.Lock(
                    cls.get_apps_json_path(apps_dir=apps_dir), 'r',
                    portalocker.LOCK_EX) as jf:
                data = jf.read()
                CartoviewApp.objects.from_json(data)
github evhub / bbopt / bbopt / optimizer.py View on Github external
def _load_data(self):
        """Load examples from data file."""
        ensure_file(self.data_file)
        with Lock(self.data_file, "rb", timeout=lock_timeout) as df:
            self._load_from(df)
github mdamien / chrome-extensions-archive / upthetop.py View on Github external
def add_to_created(ext_id, infos):
	#lock strategy fail :( - dafuq damien!
	created = get_created()
	created[ext_id] = infos
	with portalocker.Lock(CREATED_FILE, mode='a', 
			flags=portalocker.LOCK_EX, timeout=1) as f:
		json.dump(created, f, indent=2)
github shaypal5 / cachier / cachier / pickle_core.py View on Github external
def _save_cache(self, cache):
        with self.lock:
            self.cache = cache
            fpath = self._cache_fpath()
            with portalocker.Lock(fpath, mode='wb') as cache_file:
                pickle.dump(cache, cache_file)
            self._reload_cache()
github artisan-roaster-scope / artisan / src / plus / account.py View on Github external
def setAccount(account_id):
    try:
        config.logger.debug("account:setAccount(" + str(account_id) + ")")
        account_cache_semaphore.acquire(1)
        with portalocker.Lock(account_cache_lock_path, timeout=1) as _:
            with shelve.open(account_cache_path) as db:
                if account_id in db:
                    return db[account_id]
                else:
                    new_nr = len(db)
                    db[account_id] = new_nr
                    return new_nr
    except Exception as e:
        import sys
        _, _, exc_tb = sys.exc_info()
        config.logger.error("account: Exception in setAccount() line %s: %s",exc_tb.tb_lineno,e)
        return None
    finally:
        if account_cache_semaphore.available() < 1:
            account_cache_semaphore.release(1)
github mdamien / chrome-extensions-archive / upthetop.py View on Github external
def get_created():
	with portalocker.Lock(CREATED_FILE, mode='r', truncate=None,
			flags=portalocker.LOCK_SH, timeout=1) as f:
		return json.load(f)
github languitar / autosuspend / src / autosuspend / __init__.py View on Github external
lock_file:
            path of a file used for locking modifications to the `woke_up_file`
            to ensure consistency
        lock_timeout:
            time in seconds to wait for acquiring the lock file
    """

    start_time = datetime.datetime.now(datetime.timezone.utc)
    while (run_for is None) or (
        datetime.datetime.now(datetime.timezone.utc)
        < (start_time + datetime.timedelta(seconds=run_for))
    ):

        try:
            _logger.debug("New iteration, trying to acquire lock")
            with portalocker.Lock(lock_file, timeout=lock_timeout):
                _logger.debug("Acquired lock")

                just_woke_up = os.path.isfile(woke_up_file)
                if just_woke_up:
                    _logger.debug("Removing woke up file at %s", woke_up_file)
                    try:
                        os.remove(woke_up_file)
                    except FileNotFoundError:
                        _logger.warning("Just woke up file disappeared", exc_info=True)

                processor.iteration(
                    datetime.datetime.now(datetime.timezone.utc), just_woke_up
                )

        except portalocker.LockException:
            _logger.warning("Failed to acquire lock, skipping iteration", exc_info=True)
github negrinho / deep_architect / deep_architect / contrib / communicators / file_communicator.py View on Github external
def __init__(self,
                 num_procs,
                 dirname='file_comm',
                 worker_queue_file='worker_queue',
                 worker_results_prefix='worker_results_'):
        # make directory where communication files are created
        try:
            os.makedirs(dirname)
        except OSError:
            pass

        # claim a rank for the process
        lock = portalocker.Lock(os.path.join(dirname, 'init'),
                                mode='a+',
                                flags=portalocker.LOCK_EX)
        lock.acquire()
        fh = lock.fh
        fh.seek(0)
        curnum = fh.read()
        if len(curnum) is 0:
            rank = 0
        else:
            rank = int(curnum)

        if rank >= num_procs:
            raise ValueError('Number of processes > the number of workers')
        fh.seek(0)
        fh.truncate(0)
        fh.write(str(rank + 1))