def function_wrapper(*args, **kwargs): function_name = _get_function_name(func, class_name=class_name) lock_file_path = _get_function_name_lock_path( function_name, scope=scope, scope_kwargs=scope_kwargs, scope_context=scope_context ) process_id = str(os.getpid()) # to prevent dead lock when recursively calling this function # check if the same process is trying to acquire the lock _check_deadlock(lock_file_path, process_id) with file_lock(lock_file_path, remove=False, timeout=timeout) as handler: logger.info( 'process id: {0} lock function using file path: {1}' .format(process_id, lock_file_path) ) # write the process id that locked this function _write_content(handler, process_id) # call the locked function try: res = func(*args, **kwargs) finally: # clear the file _write_content(handler, None) return res
def function_wrapper(*args, **kwargs): function_name = _get_function_name(func, class_name=class_name) lock_file_path = _get_function_name_lock_path( function_name, scope=scope, scope_kwargs=scope_kwargs, scope_context=scope_context) process_id = str(os.getpid()) # to prevent dead lock when recursively calling this function # check if the same process is trying to acquire the lock _check_deadlock(lock_file_path, process_id) with file_lock(lock_file_path, remove=False, timeout=timeout) as handler: logger.info( 'process id: {} lock function using file path: {}'.format( process_id, lock_file_path)) # write the process id that locked this function _write_content(handler, process_id) # call the locked function try: res = func(*args, **kwargs) finally: # clear the file _write_content(handler, None) return res
def locking_function(function, context=None): """Lock Lock a function in combination with a context :type function: callable :type context: str :param function: the function that is intended to be locked :param context: an added context string if applicable, of a concrete lock in combination with function. """ lock_file_path = _get_function_lock_path(function, context=context) with file_lock(lock_file_path) as lock_handler: logger.info( 'locking function using file path:{}'.format(lock_file_path)) yield lock_handler
def function_wrapper(*args, **kwargs): if len(args) > 0: context = args[0] else: context = None lock_file_path = _get_context_function_lock_path(context, func) with file_lock(lock_file_path): logger.info( 'lock function using file path:{}'.format(lock_file_path)) res = func(*args, **kwargs) return res
def locking_function(function, context=None): """Lock Lock a function in combination with a context :type function: callable :type context: str :param function: the function that is intended to be locked :param context: an added context string if applicable, of a concrete lock in combination with function. """ lock_file_path = _get_function_lock_path(function, context=context) with file_lock(lock_file_path) as lock_handler: logger.info('locking function using file path:{}'.format( lock_file_path)) yield lock_handler
def function_wrapper(*args, **kwargs): if len(args) > 0: context = args[0] else: context = None lock_file_path = _get_context_function_lock_path(context, func) with file_lock(lock_file_path): logger.info('lock function using file path:{}'.format( lock_file_path)) res = func(*args, **kwargs) return res
def locking_function( function, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=LOCK_DEFAULT_TIMEOUT, ): """Lock a function in combination with a scope and scope_context. Any parallel pytest xdist worker will wait for this function to finish. :type function: callable :type scope: str or callable :type scope_kwargs: dict :type scope_context: str :type timeout: int :param function: the function that is intended to be locked :param scope: this parameter will define the namespace of locking :param scope_context: an added context string if applicable, of a concrete lock in combination with scope and function. :param scope_kwargs: kwargs to be passed to scope if is a callable :param timeout: the time in seconds to wait for acquiring the lock """ if not getattr(function, '__function_locked__', False): raise FunctionLockerError( 'Cannot ensure locking when using a non locked function') class_name = getattr(function, '__class_name__', None) function_name = _get_function_name(function, class_name=class_name) lock_file_path = _get_function_name_lock_path(function_name, scope=scope, scope_kwargs=scope_kwargs, scope_context=scope_context) process_id = str(os.getpid()) # to prevent dead lock when recursively calling this function # check if the same process is trying to acquire the lock _check_deadlock(lock_file_path, process_id) with file_lock(lock_file_path, remove=False, timeout=timeout) as handler: logger.info( 'process id: {} - lock function name:{} - using file path: {}'. format(process_id, function_name, lock_file_path)) # write the process id that locked this function _write_content(handler, process_id) # let the locked code run try: yield handler finally: # clear the file _write_content(handler, None)
def locking_function(function, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=LOCK_DEFAULT_TIMEOUT): """Lock a function in combination with a scope and scope_context. Any parallel pytest xdist worker will wait for this function to finish. :type function: callable :type scope: str or callable :type scope_kwargs: dict :type scope_context: str :type timeout: int :param function: the function that is intended to be locked :param scope: this parameter will define the namespace of locking :param scope_context: an added context string if applicable, of a concrete lock in combination with scope and function. :param scope_kwargs: kwargs to be passed to scope if is a callable :param timeout: the time in seconds to wait for acquiring the lock """ if not getattr(function, '__function_locked__', False): raise FunctionLockerError( 'Cannot ensure locking when using a non locked function') class_name = getattr(function, '__class_name__', None) function_name = _get_function_name(function, class_name=class_name) lock_file_path = _get_function_name_lock_path( function_name, scope=scope, scope_kwargs=scope_kwargs, scope_context=scope_context ) process_id = str(os.getpid()) # to prevent dead lock when recursively calling this function # check if the same process is trying to acquire the lock _check_deadlock(lock_file_path, process_id) with file_lock(lock_file_path, remove=False, timeout=timeout) as handler: logger.info( 'process id: {0} - lock function name:{1} - using file path: {2}' .format(process_id, function_name, lock_file_path) ) # write the process id that locked this function _write_content(handler, process_id) # let the locked code run try: yield handler finally: # clear the file _write_content(handler, None)
def lock(self, key): """Return the storage locker context manager""" lock_key = '{}.lock'.format(key) return file_lock(self.get_key_file_path(lock_key), remove=False, timeout=self._lock_timeout)
def lock(self, key): """Return the storage locker context manager""" lock_key = f'{key}.lock' return file_lock(self.get_key_file_path(lock_key), remove=False, timeout=self._lock_timeout)