我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用threading.Thread._Thread__stop()。
def timelimited(timeout): """ :param timeout:?????int/float? ????????????????????????? ??????????,?Rasie??????????? """ def decorator(function): def decorator2(*args, **kwargs): class TimeLimited(Thread): def __init__(self, _error=None, ): Thread.__init__(self) self._error = _error self.result = None def run(self): try: self.result = function(*args, **kwargs) except Exception, e: self._error = e def _stop(self): if self.isAlive(): ThreadStop = Thread._Thread__stop ThreadStop(self) t = TimeLimited() t.setDaemon(True) # ???? t.start() t.join(timeout) if isinstance(t._error, TimeoutException): t._stop() return 'ERROR -1' # ?? if t.isAlive(): t._stop() return 'ERROR -1' # ?? if t._error is None: t._stop() return t.result return decorator2 return decorator
def timelimited(timeout): def decorator(function): def decorator2(*args, **kwargs): class TimeLimited(Thread): def __init__(self, _error=None, ): Thread.__init__(self) self._error = _error self.result = None def run(self): try: self.result = function(*args, **kwargs) except Exception, e: self._error = e def _stop(self): if self.isAlive(): ThreadStop = Thread._Thread__stop ThreadStop(self) t = TimeLimited() t.setDaemon(True) # ???? t.start() t.join(timeout) if isinstance(t._error, TimeoutException): t._stop() return 'ERROR -1' # ?? if t.isAlive(): t._stop() return 'ERROR -1' # ?? if t._error is None: t._stop() return t.result return decorator2 return decorator