def access(self): """ Request if the give thread is allowed to access the resource. """ # self._debug("%s access()" % _currentThread().name) if not self.isLock(): self._debug("There is no owner of the lock, %s can pass" % _currentThread().name) return True elif self._isOwner(): self._when = _datetime.now() self._debug("The owner is who is asking (%s), " "renew its booking" % (_currentThread().name)) return True else: return False
def _isOwner(self): isOwner = self._owner == _currentThread().name # if isOwner: # self._debug("%s is the owner" % (_currentThread().name)) # else: # self._debug("%s is NOT the owner" % (_currentThread().name)) return isOwner
def _doLock(self, timeout=None): if self._hasOwner(): raise RuntimeError("Try to lock when not yet released") self._owner = _currentThread().name self._when = _datetime.now() self.expirationTime = timeout self._info("%s has take the lock (expiration time %s)" % (self.owner, self.expirationTime))
def __stop(self): current = _currentThread() target = self if current is target: self.should_die() self.__running = 0 else: self.__queue.put(PendingResult(None, None, self.__stop)) _sleep(0.001) return
def request(self, timeout=None): """ Request to book the access. If its free (or has expired a previous one) do it, else reject (even if the owner recalls it). """ # self._debug("%s request()" % _currentThread().name) if not self._hasOwner() or self._hasExpired(): self._doLock(timeout) return True else: self._warning("%s request the lock when already is" % (_currentThread().name)) return False
def release(self): """ Only the owner can release the lock. """ # self._debug("%s release()" % _currentThread().name) if self._isOwner(): self._debug("%s releases the lock" % self._owner) self._doRelease() return True else: self._error("%s is NOT allowed to release %s's lock" % (_currentThread().name, self._owner)) return False
def handler(exc=None, passthru=True): if exc is None: exc = sys.exc_info() TEXT = _format_exc(exc) try: _thread = _currentThread() TEXT = ("Exception in thread %s:\n" % _thread.name) + TEXT except: pass CMD = len(sys.argv) > 1 and sys.argv[1].endswith('.py') and sys.argv[1] or sys.argv[0] SUBJECT = CMD+': '+str(exc[1]).replace('\r','').replace('\n','') HEADERS = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n' % (EMAIL_FROM, EMAIL_TO, SUBJECT) _SMTP('localhost').sendmail(EMAIL_FROM, [EMAIL_TO], HEADERS+TEXT) if passthru: sys.__excepthook__(*exc) return TEXT
def printInfo(msg, level=0, lock=None, top=False, bottom=False): if lock is None: print("%s%s" % ("\t" * level, msg)) else: with lock: tab = "\t" * level msg = "Thread %s: %s" % (_currentThread().name, msg) if top or bottom: if top: msg = "%s%s\n%s%s" % (tab, "-" * len(msg), tab, msg) elif bottom: msg = "%s%s\n%s%s\n" % (tab, msg, tab, "-" * len(msg)) else: msg = "%s%s" % (tab, msg) print(msg)
def printInfo(msg, level=0, lock=None, top=False, bottom=False): if lock is None: print("%s%s" % ("\t"*level, msg)) else: with lock: tab = "\t"*level msg = "Thread %s: %s" % (_currentThread().name, msg) if top or bottom: if top: msg = "%s%s\n%s%s" % (tab, "-"*len(msg), tab, msg) elif bottom: msg = "%s%s\n%s%s\n" % (tab, msg, tab, "-"*len(msg)) else: msg = "%s%s" % (tab, msg) print(msg)
def _threadId(self): return _currentThread().getName()
def __currentDict(self, _currentThread = _currentThread): try: return self.__thread_dict[_currentThread()] except KeyError: self.__thread_dict[_currentThread()] = result = {} return result