def RLock(): ''' Factory function: if `cs.logutils.loginfo.level<=logging.DEBUG` then return a `DebuggingRLock`, otherwise a `threading.RLock`. ''' if not ifdebug(): return threading.RLock() filename, lineno = inspect.stack()[1][1:3] return DebuggingRLock({'filename': filename, 'lineno': lineno})
def __init__(self, capacity, name=None, inboundCapacity=0, retry_delay=None): ''' Initialise the Later instance. Parameters: * `capacity`: resource contraint on this Later; if an int, it is used to size a Semaphore to constrain the number of dispatched functions which may be in play at a time; if not an int it is presumed to be a suitable Semaphore-like object, perhaps shared with other subsystems. * `name`: optional identifying name for this instance. * `inboundCapacity`: if >0, used as a limit on the number of undispatched functions that may be queued up; the default is 0 (no limit). Calls to submit functions when the inbound limit is reached block until some functions are dispatched. * `retry_delay`: time delay for requeued functions. Default: `DEFAULT_RETRY_DELAY`. ''' if name is None: name = "Later-%d" % (seq(),) if ifdebug(): import inspect # pylint: disable=import-outside-toplevel filename, lineno = inspect.stack()[1][1:3] name = "%s[%s:%d]" % (name, filename, lineno) debug( "Later.__init__(capacity=%s, inboundCapacity=%s, name=%s)", capacity, inboundCapacity, name ) if retry_delay is None: retry_delay = DEFAULT_RETRY_DELAY self.capacity = capacity self.inboundCapacity = inboundCapacity self.retry_delay = retry_delay self.name = name self._lock = Lock() self.outstanding = set() # dispatched but uncompleted LateFunctions self.delayed = set() # unqueued, delayed until specific time self.pending = [] # undispatched LateFunctions, a heap self.running = set() # running LateFunctions # counter tracking jobs queued or active self._state = "" self.logger = None # reporting; see logTo() method self._priority = (0,) self._timerQ = None # queue for delayed requests; instantiated at need # inbound requests queue self._finished = None
def __init__(self, start=None, end=None, debug=None): ''' Initialise the Range. Called with `start` and `end`, these specify the initial `Span` of the `Range`. If called with just one argument that argument instead be an iterable of integer values comprising the values in the `Range`. ''' if debug is None: debug = ifdebug() self._debug = debug self._spans = [] if start is not None: if end is None: # "start" must be an iterable of ints for substart, subend in spans(start): self.add_span(substart, subend) else: self.add_span(start, end)
def inner(*a, **kw): if not force and not ifdebug(): return f(*a, **kw) filename, lineno = inspect.stack()[1][1:3] n = seq() R = Result() T = threading.Thread(target=_debug_watcher, args=(filename, lineno, n, f.__name__, R)) T.daemon = True T.start() debug("%s:%d: [%d] call %s(*%r, **%r)", filename, lineno, n, f.__name__, a, kw) start = time.time() try: retval = f(*a, **kw) except Exception as e: error("EXCEPTION from %s(*%s, **%s): %s", f, a, kw, e) raise end = time.time() debug("%s:%d: [%d] called %s, elapsed %gs, got %r", filename, lineno, n, f.__name__, end - start, retval) R.put(retval) return retval
def Thread(*a, **kw): if not ifdebug(): return threading.Thread(*a, **kw) filename, lineno = inspect.stack()[1][1:3] return DebuggingThread({'filename': filename, 'lineno': lineno}, *a, **kw)
def sqlAlchemy(secret, scheme, login, password, host, database): import sqlalchemy return sqlalchemy.create_engine( '%s://%s:%s@%s/%s' % (scheme, secret.LOGIN, secret.PASSWORD, secret.HOST, secret.DATABASE), echo=ifdebug())