locking functions
Bases: builtins.object
class used to lock an entire object. UNTESTED
locking decorator.
function locking decorator
# fbf/utils/locking.py # # """ locking functions """
from .exception import handle_exception from .trace import whichmodule from .lockmanager import lockmanager, rlockmanager from fbf.lib.threads import getname
import logging import sys import _thread
class Locked(object): """ class used to lock an entire object. UNTESTED""" def __getattribute__(self, attr): where = whichmodule(1) logging.debug('locking - locking on %s' % where) rlockmanager.acquire(object) res = None try: res = super(Locked, self).__getattribute__(attr) finally: rlockmanager.release(object) return res
def lockdec(lock): """ locking decorator. """ def locked(func): """ locking function for %s """ % str(func) def lockedfunc(*args, **kwargs): """ the locked function. """ where = whichmodule(2) logging.debug('locking - locking on %s' % where) lock.acquire() res = None try: res = func(*args, **kwargs) finally: try: lock.release() logging.debug('locking - releasing %s' % where) except: pass return res return lockedfunc return locked
def lock_object(object): try: locktarget = object.repr() except: locktarget = repr(object) lockmanager.acquire(locktarget) def release_object(object): try: locktarget = object.repr() except: locktarget = repr(object) lockmanager.release(locktarget) def locked(func): """ function locking decorator """ def lockedfunc(*args, **kwargs): """ the locked function. """ where = getname(str(func)) try: rlockmanager.acquire(where) res = func(*args, **kwargs) finally: rlockmanager.release(where) return res return lockedfunc globallock = _thread.allocate_lock() globallocked = lockdec(globallock)