Skip to content

Commit

Permalink
fix github issue #49 is_alive compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmgn committed Nov 10, 2022
1 parent 40e2663 commit ab2035d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cup/services/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cup/shell/oper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
)
Expand Down
23 changes: 20 additions & 3 deletions cup/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
cup thread module
"""

__all__ = ['async_raise', 'CupThread', 'RWLock']
__all__ = ['async_raise', 'CupThread', 'RWLock', 'thread_alive']


import threading
Expand All @@ -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;
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ab2035d

Please sign in to comment.