54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
|
import inspect
|
||
|
import ctypes
|
||
|
import threading
|
||
|
|
||
|
class MyThreadFunc(object):
|
||
|
'''
|
||
|
手动终止线程的方法
|
||
|
'''
|
||
|
def __init__(self, func, argsTup):
|
||
|
self.myThread = threading.Thread(target=func, args=argsTup)
|
||
|
self.daemon = True
|
||
|
|
||
|
def start(self):
|
||
|
print('线程启动')
|
||
|
self.result = self.myThread.start()
|
||
|
|
||
|
def join(self):
|
||
|
self.myThread.join()
|
||
|
|
||
|
def get_result(self):
|
||
|
try:
|
||
|
return self.result
|
||
|
except Exception as e:
|
||
|
return None
|
||
|
|
||
|
def state(self):
|
||
|
status = self.myThread.is_alive()
|
||
|
print('线程状态: {0}'.format(status))
|
||
|
return status
|
||
|
|
||
|
def stop(self):
|
||
|
print('线程终止')
|
||
|
# self.myThread.join()
|
||
|
try:
|
||
|
for i in range(5):
|
||
|
self._async_raise(self.myThread.ident, SystemExit)
|
||
|
# time.sleep(1)
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
|
||
|
def _async_raise(self, tid, exctype):
|
||
|
"""raises the exception, performs cleanup if needed"""
|
||
|
tid = ctypes.c_long(tid)
|
||
|
if not inspect.isclass(exctype):
|
||
|
exctype = type(exctype)
|
||
|
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
|
||
|
if res == 0:
|
||
|
raise ValueError("invalid thread id")
|
||
|
elif res != 1:
|
||
|
# """if it returns a number greater than one, you're in trouble,
|
||
|
# and you should call it again with exc=NULL to revert the effect"""
|
||
|
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
|
||
|
raise SystemError("PyThreadState_SetAsyncExc failed")
|