Best Python code snippet using avocado_python
filelock.py
Source:filelock.py  
...383        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC384        fd = os.open(self._lock_file, open_mode)385386        try:387            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)388        except (IOError, OSError):389            os.close(fd)390        else:391            self._lock_file_fd = fd392        return None393394    def _release(self):395        # Do not remove the lockfile:396        #397        #   https://github.com/benediktschmitt/py-filelock/issues/31398        #   https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition399        fd = self._lock_file_fd400        self._lock_file_fd = None401        fcntl.flock(fd, fcntl.LOCK_UN)402        os.close(fd)403        return None404405# Soft lock406# ~~~~~~~~~407408class SoftFileLock(BaseFileLock):409    """410    Simply watches the existence of the lock file.411    """412413    def _acquire(self):414        open_mode = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC415        try:
...lockutils.py
Source:lockutils.py  
...88                # Using non-blocking locks since green threads are not89                # patched to deal with blocking locking calls.90                # Also upon reading the MSDN docs for locking(), it seems91                # to have a laughable 10 attempts "blocking" mechanism.92                self.trylock()93                LOG.debug('Got file lock "%s"', self.fname)94                return True95            except IOError as e:96                if e.errno in (errno.EACCES, errno.EAGAIN):97                    # external locks synchronise things like iptables98                    # updates - give it some time to prevent busy spinning99                    time.sleep(0.01)100                else:101                    raise threading.ThreadError(_("Unable to acquire lock on"102                                                  " `%(filename)s` due to"103                                                  " %(exception)s") %104                                                {'filename': self.fname,105                                                    'exception': e})106107    def __enter__(self):108        self.acquire()109        return self110111    def release(self):112        try:113            self.unlock()114            self.lockfile.close()115            LOG.debug('Released file lock "%s"', self.fname)116        except IOError:117            LOG.exception(_LE("Could not release the acquired lock `%s`"),118                          self.fname)119120    def __exit__(self, exc_type, exc_val, exc_tb):121        self.release()122123    def exists(self):124        return os.path.exists(self.fname)125126    def trylock(self):127        raise NotImplementedError()128129    def unlock(self):130        raise NotImplementedError()131132133class _WindowsLock(_FileLock):134    def trylock(self):135        msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_NBLCK, 1)136137    def unlock(self):138        msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_UNLCK, 1)139140141class _FcntlLock(_FileLock):142    def trylock(self):143        fcntl.lockf(self.lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB)144145    def unlock(self):146        fcntl.lockf(self.lockfile, fcntl.LOCK_UN)147148149class _PosixLock(object):150    def __init__(self, name):151        # Hash the name because it's not valid to have POSIX semaphore152        # names with things like / in them. Then use base64 to encode153        # the digest() instead taking the hexdigest() because the154        # result is shorter and most systems can't have shm sempahore155        # names longer than 31 characters.156        h = hashlib.sha1()157        h.update(name.encode('ascii'))158        self.name = str((b'/' + base64.urlsafe_b64encode(159            h.digest())).decode('ascii'))160161    def acquire(self, timeout=None):162        self.semaphore = posix_ipc.Semaphore(self.name,163                                             flags=posix_ipc.O_CREAT,164                                             initial_value=1)165        self.semaphore.acquire(timeout)166        return self167168    def __enter__(self):169        self.acquire()170        return self171172    def release(self):173        self.semaphore.release()174        self.semaphore.close()175176    def __exit__(self, exc_type, exc_val, exc_tb):177        self.release()178179    def exists(self):180        try:181            semaphore = posix_ipc.Semaphore(self.name)182        except posix_ipc.ExistentialError:183            return False184        else:185            semaphore.close()186        return True187188189if os.name == 'nt':190    import msvcrt191    InterProcessLock = _WindowsLock192    FileLock = _WindowsLock193else:194    import base64195    import fcntl196    import hashlib197198    import posix_ipc199    InterProcessLock = _PosixLock200    FileLock = _FcntlLock201202_semaphores = weakref.WeakValueDictionary()203_semaphores_lock = threading.Lock()204205206def _get_lock_path(name, lock_file_prefix, lock_path=None):207    # NOTE(mikal): the lock name cannot contain directory208    # separators209    name = name.replace(os.sep, '_')210    if lock_file_prefix:211        sep = '' if lock_file_prefix.endswith('-') else '-'212        name = '%s%s%s' % (lock_file_prefix, sep, name)213214    local_lock_path = lock_path or CONF.lock_path215216    if not local_lock_path:217        # NOTE(bnemec): Create a fake lock path for posix locks so we don't218        # unnecessarily raise the RequiredOptError below.219        if InterProcessLock is not _PosixLock:220            raise cfg.RequiredOptError('lock_path')221        local_lock_path = 'posixlock:/'222223    return os.path.join(local_lock_path, name)224225226def external_lock(name, lock_file_prefix=None, lock_path=None):227    LOG.debug('Attempting to grab external lock "%(lock)s"',228              {'lock': name})229230    lock_file_path = _get_lock_path(name, lock_file_prefix, lock_path)231232    # NOTE(bnemec): If an explicit lock_path was passed to us then it233    # means the caller is relying on file-based locking behavior, so234    # we can't use posix locks for those calls.235    if lock_path:236        return FileLock(lock_file_path)237    return InterProcessLock(lock_file_path)238239240def remove_external_lock_file(name, lock_file_prefix=None):241    """Remove an external lock file when it's not used anymore242    This will be helpful when we have a lot of lock files243    """244    with internal_lock(name):245        lock_file_path = _get_lock_path(name, lock_file_prefix)246        try:247            os.remove(lock_file_path)248        except OSError:249            LOG.info(_LI('Failed to remove file %(file)s'),250                     {'file': lock_file_path})251252253def internal_lock(name):254    with _semaphores_lock:255        try:256            sem = _semaphores[name]257        except KeyError:258            sem = threading.Semaphore()259            _semaphores[name] = sem260261    LOG.debug('Got semaphore "%(lock)s"', {'lock': name})262    return sem263264265@contextlib.contextmanager266def lock(name, lock_file_prefix=None, external=False, lock_path=None):267    """Context based lock268269    This function yields a `threading.Semaphore` instance (if we don't use270    eventlet.monkey_patch(), else `semaphore.Semaphore`) unless external is271    True, in which case, it'll yield an InterProcessLock instance.272273    :param lock_file_prefix: The lock_file_prefix argument is used to provide274      lock files on disk with a meaningful prefix.275276    :param external: The external keyword argument denotes whether this lock277      should work across multiple processes. This means that if two different278      workers both run a method decorated with @synchronized('mylock',279      external=True), only one of them will execute at a time.280    """281    int_lock = internal_lock(name)282    with int_lock:283        if external and not CONF.disable_process_locking:284            ext_lock = external_lock(name, lock_file_prefix, lock_path)285            with ext_lock:286                yield ext_lock287        else:288            yield int_lock289    LOG.debug('Released semaphore "%(lock)s"', {'lock': name})290291292def synchronized(name, lock_file_prefix=None, external=False, lock_path=None):293    """Synchronization decorator.294295    Decorating a method like so::296297        @synchronized('mylock')298        def foo(self, *args):299           ...300301    ensures that only one thread will execute the foo method at a time.302303    Different methods can share the same lock::304305        @synchronized('mylock')306        def foo(self, *args):307           ...308309        @synchronized('mylock')310        def bar(self, *args):311           ...312313    This way only one of either foo or bar can be executing at a time.314    """315316    def wrap(f):317        @functools.wraps(f)318        def inner(*args, **kwargs):319            try:320                with lock(name, lock_file_prefix, external, lock_path):321                    LOG.debug('Got semaphore / lock "%(function)s"',322                              {'function': f.__name__})323                    return f(*args, **kwargs)324            finally:325                LOG.debug('Semaphore / lock released "%(function)s"',326                          {'function': f.__name__})327        return inner328    return wrap329330331def synchronized_with_prefix(lock_file_prefix):332    """Partial object generator for the synchronization decorator.333334    Redefine @synchronized in each project like so::
...LockHandler.ts
Source:LockHandler.ts  
1import { Dirnames } from './utils/types';2const JoplinError = require('lib/JoplinError');3const { time } = require('lib/time-utils');4const { fileExtension, filename } = require('lib/path-utils.js');5export enum LockType {6	None = '',7	Sync = 'sync',8	Exclusive = 'exclusive',9}10export interface Lock {11	type: LockType,12	clientType: string,13	clientId: string,14	updatedTime?: number,15}16interface RefreshTimer {17	id: any,18	inProgress: boolean19}20interface RefreshTimers {21	[key:string]: RefreshTimer;22}23export interface LockHandlerOptions {24	autoRefreshInterval?: number,25	lockTtl?: number,26}27export default class LockHandler {28	private api_:any = null;29	private refreshTimers_:RefreshTimers = {};30	private autoRefreshInterval_:number = 1000 * 60;31	private lockTtl_:number = 1000 * 60 * 3;32	constructor(api:any, options:LockHandlerOptions = null) {33		if (!options) options = {};34		this.api_ = api;35		if ('lockTtl' in options) this.lockTtl_ = options.lockTtl;36		if ('autoRefreshInterval' in options) this.autoRefreshInterval_ = options.autoRefreshInterval;37	}38	public get lockTtl():number {39		return this.lockTtl_;40	}41	// Should only be done for testing purposes since all clients should42	// use the same lock max age.43	public set lockTtl(v:number) {44		this.lockTtl_ = v;45	}46	private lockFilename(lock:Lock) {47		return `${[lock.type, lock.clientType, lock.clientId].join('_')}.json`;48	}49	private lockTypeFromFilename(name:string):LockType {50		const ext = fileExtension(name);51		if (ext !== 'json') return LockType.None;52		if (name.indexOf(LockType.Sync) === 0) return LockType.Sync;53		if (name.indexOf(LockType.Exclusive) === 0) return LockType.Exclusive;54		return LockType.None;55	}56	private lockFilePath(lock:Lock) {57		return `${Dirnames.Locks}/${this.lockFilename(lock)}`;58	}59	private lockFileToObject(file:any):Lock {60		const p = filename(file.path).split('_');61		return {62			type: p[0],63			clientType: p[1],64			clientId: p[2],65			updatedTime: file.updated_time,66		};67	}68	async locks(lockType:LockType = null):Promise<Lock[]> {69		const result = await this.api_.list(Dirnames.Locks);70		if (result.hasMore) throw new Error('hasMore not handled'); // Shouldn't happen anyway71		const output = [];72		for (const file of result.items) {73			const type = this.lockTypeFromFilename(file.path);74			if (type === LockType.None) continue;75			if (lockType && type !== lockType) continue;76			const lock = this.lockFileToObject(file);77			output.push(lock);78		}79		return output;80	}81	private lockIsActive(lock:Lock):boolean {82		return Date.now() - lock.updatedTime < this.lockTtl;83	}84	async hasActiveLock(lockType:LockType, clientType:string = null, clientId:string = null) {85		const lock = await this.activeLock(lockType, clientType, clientId);86		return !!lock;87	}88	// Finds if there's an active lock for this clientType and clientId and returns it.89	// If clientType and clientId are not specified, returns the first active lock90	// of that type instead.91	async activeLock(lockType:LockType, clientType:string = null, clientId:string = null) {92		const locks = await this.locks(lockType);93		if (lockType === LockType.Exclusive) {94			const activeLocks = locks95				.slice()96				.filter((lock:Lock) => this.lockIsActive(lock))97				.sort((a:Lock, b:Lock) => {98					if (a.updatedTime === b.updatedTime) {99						return a.clientId < b.clientId ? -1 : +1;100					}101					return a.updatedTime < b.updatedTime ? -1 : +1;102				});103			if (!activeLocks.length) return null;104			const activeLock = activeLocks[0];105			if (clientType && clientType !== activeLock.clientType) return null;106			if (clientId && clientId !== activeLock.clientId) return null;107			return activeLock;108		} else if (lockType === LockType.Sync) {109			for (const lock of locks) {110				if (clientType && lock.clientType !== clientType) continue;111				if (clientId && lock.clientId !== clientId) continue;112				if (this.lockIsActive(lock)) return lock;113			}114			return null;115		}116		throw new Error(`Unsupported lock type: ${lockType}`);117	}118	private async saveLock(lock:Lock) {119		await this.api_.put(this.lockFilePath(lock), JSON.stringify(lock));120	}121	// This is for testing only122	public async saveLock_(lock:Lock) {123		return this.saveLock(lock);124	}125	private async acquireSyncLock(clientType:string, clientId:string):Promise<Lock> {126		try {127			let isFirstPass = true;128			while (true) {129				const [exclusiveLock, syncLock] = await Promise.all([130					this.activeLock(LockType.Exclusive),131					this.activeLock(LockType.Sync, clientType, clientId),132				]);133				if (exclusiveLock) {134					throw new JoplinError(`Cannot acquire sync lock because the following client has an exclusive lock on the sync target: ${this.lockToClientString(exclusiveLock)}`, 'hasExclusiveLock');135				}136				if (syncLock) {137					// Normally the second pass should happen immediately afterwards, but if for some reason138					// (slow network, etc.) it took more than 10 seconds then refresh the lock.139					if (isFirstPass || Date.now() - syncLock.updatedTime > 1000 * 10) {140						await this.saveLock(syncLock);141					}142					return syncLock;143				}144				// Something wrong happened, which means we saved a lock but we didn't read145				// it back. Could be application error or server issue.146				if (!isFirstPass) throw new Error('Cannot acquire sync lock: either the lock could be written but not read back. Or it was expired before it was read again.');147				await this.saveLock({148					type: LockType.Sync,149					clientType: clientType,150					clientId: clientId,151				});152				isFirstPass = false;153			}154		} catch (error) {155			await this.releaseLock(LockType.Sync, clientType, clientId);156			throw error;157		}158	}159	private lockToClientString(lock:Lock):string {160		return `(${lock.clientType} #${lock.clientId})`;161	}162	private async acquireExclusiveLock(clientType:string, clientId:string, timeoutMs:number = 0):Promise<Lock> {163		// The logic to acquire an exclusive lock, while avoiding race conditions is as follow:164		//165		// - Check if there is a lock file present166		//167		// - If there is a lock file, see if I'm the one owning it by checking that its content has my identifier.168		// - If that's the case, just write to the data file then delete the lock file.169		// - If that's not the case, just wait a second or a small random length of time and try the whole cycle again-.170		//171		// -If there is no lock file, create one with my identifier and try the whole cycle again to avoid race condition (re-check that the lock file is really mine)-.172		const startTime = Date.now();173		async function waitForTimeout() {174			if (!timeoutMs) return false;175			const elapsed = Date.now() - startTime;176			if (timeoutMs && elapsed < timeoutMs) {177				await time.sleep(2);178				return true;179			}180			return false;181		}182		try {183			while (true) {184				const [activeSyncLock, activeExclusiveLock] = await Promise.all([185					this.activeLock(LockType.Sync),186					this.activeLock(LockType.Exclusive),187				]);188				if (activeSyncLock) {189					if (await waitForTimeout()) continue;190					throw new JoplinError(`Cannot acquire exclusive lock because the following clients have a sync lock on the target: ${this.lockToClientString(activeSyncLock)}`, 'hasSyncLock');191				}192				if (activeExclusiveLock) {193					if (activeExclusiveLock.clientId === clientId) {194						// Save it again to refresh the timestamp195						await this.saveLock(activeExclusiveLock);196						return activeExclusiveLock;197					} else {198						// If there's already an exclusive lock, wait for it to be released199						if (await waitForTimeout()) continue;200						throw new JoplinError(`Cannot acquire exclusive lock because the following client has an exclusive lock on the sync target: ${this.lockToClientString(activeExclusiveLock)}`, 'hasExclusiveLock');201					}202				} else {203					// If there's not already an exclusive lock, acquire one204					// then loop again to check that we really got the lock205					// (to prevent race conditions)206					await this.saveLock({207						type: LockType.Exclusive,208						clientType: clientType,209						clientId: clientId,210					});211					await time.msleep(100);212				}213			}214		} catch (error) {215			await this.releaseLock(LockType.Exclusive, clientType, clientId);216			throw error;217		}218	}219	private autoLockRefreshHandle(lock:Lock) {220		return [lock.type, lock.clientType, lock.clientId].join('_');221	}222	startAutoLockRefresh(lock:Lock, errorHandler:Function):string {223		const handle = this.autoLockRefreshHandle(lock);224		if (this.refreshTimers_[handle]) {225			throw new Error(`There is already a timer refreshing this lock: ${handle}`);226		}227		this.refreshTimers_[handle] = {228			id: null,229			inProgress: false,230		};231		this.refreshTimers_[handle].id = setInterval(async () => {232			if (this.refreshTimers_[handle].inProgress) return;233			const defer = () => {234				if (!this.refreshTimers_[handle]) return;235				this.refreshTimers_[handle].inProgress = false;236			};237			this.refreshTimers_[handle].inProgress = true;238			let error = null;239			const hasActiveLock = await this.hasActiveLock(lock.type, lock.clientType, lock.clientId);240			if (!this.refreshTimers_[handle]) return defer(); // Timeout has been cleared241			if (!hasActiveLock) {242				error = new JoplinError('Lock has expired', 'lockExpired');243			} else {244				try {245					await this.acquireLock(lock.type, lock.clientType, lock.clientId);246					if (!this.refreshTimers_[handle]) return defer(); // Timeout has been cleared247				} catch (e) {248					error = e;249				}250			}251			if (error) {252				if (this.refreshTimers_[handle]) {253					clearInterval(this.refreshTimers_[handle].id);254					delete this.refreshTimers_[handle];255				}256				errorHandler(error);257			}258			defer();259		}, this.autoRefreshInterval_);260		return handle;261	}262	stopAutoLockRefresh(lock:Lock) {263		const handle = this.autoLockRefreshHandle(lock);264		if (!this.refreshTimers_[handle]) {265			// Should not throw an error because lock may have been cleared in startAutoLockRefresh266			// if there was an error.267			// throw new Error(`There is no such lock being auto-refreshed: ${this.lockToString(lock)}`);268			return;269		}270		clearInterval(this.refreshTimers_[handle].id);271		delete this.refreshTimers_[handle];272	}273	async acquireLock(lockType:LockType, clientType:string, clientId:string, timeoutMs:number = 0):Promise<Lock> {274		if (lockType === LockType.Sync) {275			return this.acquireSyncLock(clientType, clientId);276		} else if (lockType === LockType.Exclusive) {277			return this.acquireExclusiveLock(clientType, clientId, timeoutMs);278		} else {279			throw new Error(`Invalid lock type: ${lockType}`);280		}281	}282	async releaseLock(lockType:LockType, clientType:string, clientId:string) {283		await this.api_.delete(this.lockFilePath({284			type: lockType,285			clientType: clientType,286			clientId: clientId,287		}));288	}...__init__.py
Source:__init__.py  
...200        """201        Return True if this object is locking the file.202        """203        raise NotImplemented("implement in subclass")204    def break_lock(self):205        """206        Remove a lock.  Useful if a locking thread failed to unlock.207        """208        raise NotImplemented("implement in subclass")209    def __repr__(self):210        return "<%s: %r -- %r>" % (self.__class__.__name__, self.unique_name,211                                   self.path)212def _fl_helper(cls, mod, *args, **kwds):213    warnings.warn("Import from %s module instead of lockfile package" % mod,214                  DeprecationWarning, stacklevel=2)215    # This is a bit funky, but it's only for awhile.  The way the unit tests216    # are constructed this function winds up as an unbound method, so it217    # actually takes three args, not two.  We want to toss out self.218    if not isinstance(args[0], str):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
