diff --git a/cup/services/threadpool.py b/cup/services/threadpool.py index f9a3f08..dce888b 100644 --- a/cup/services/threadpool.py +++ b/cup/services/threadpool.py @@ -304,7 +304,7 @@ def stop(self, force_stop=False): times = 0 while not retry and (times <= 100): for thd in self._threads: - if thd.isAlive(): + if thread.thread_alive(thd): thd.terminate() retry = True time.sleep(0.1) @@ -326,7 +326,7 @@ def try_stop(self, check_interval=0.1): thd.join(check_interval) for thd in threads: - if thd.isAlive(): + if thread.thread_alive(thd): return False return True diff --git a/cup/shell/oper.py b/cup/shell/oper.py index 1013895..cff391e 100644 --- a/cup/shell/oper.py +++ b/cup/shell/oper.py @@ -26,6 +26,7 @@ import cup from cup import err from cup import log +from cup import thread from cup import platforms from cup import decorators @@ -573,7 +574,7 @@ def _pipe_asshell(cmd): ) cmdthd.start() cmdthd.join(timeout) - if cmdthd.isAlive(): + if thread.thread_alive(cmdthd): str_warn = ( 'Shell "%s"execution timout:%d. Killed it' % (cmd, timeout) ) diff --git a/cup/thread.py b/cup/thread.py index ce7b665..2cdbab3 100644 --- a/cup/thread.py +++ b/cup/thread.py @@ -7,7 +7,7 @@ cup thread module """ -__all__ = ['async_raise', 'CupThread', 'RWLock'] +__all__ = ['async_raise', 'CupThread', 'RWLock', 'thread_alive'] import threading @@ -33,6 +33,22 @@ def async_raise(tid, exctype): ) +def thread_alive(obj): + """ + check if thread is alive. Py2 py3 compatible + + :raise Exception: + if the obejct does not have attr obj.is_alive and obj.isAlive, + the lib will raise ValueError + """ + if hasattr(obj, 'is_alive'): + return obj.is_alive() + elif hasattr(obj, 'isAlive'): + return obj.isAlive() + else: + raise ValueError('obj is not a object instance of threading.Thread') + + class CupThread(threading.Thread): """ CupThread is a sub-class inherited from threading.Thread; @@ -52,11 +68,12 @@ class CupThread(threading.Thread): response to the signals! In other words, it may not raise any exception/terminate even though cup has send a CupThread signal! """ + def get_my_tid(self): """ return thread id """ - if not self.isAlive(): + if not thread_alive(self): cup.log.warn('the thread is not active') return None # do we have it cached? @@ -94,7 +111,7 @@ def terminate(self, times=15): retry times until call for failure. """ cnt = 0 - while self.isAlive(): + while thread_alive(self): self.raise_exc(cup.err.ThreadTermException) time.sleep(1) cnt += 1