-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyThread.py
57 lines (44 loc) · 1.51 KB
/
myThread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#coding:UTF-8
import time
import ctypes
import inspect
import threading
class myThread(object):
def __init__(self):
self.threadList = []
self.threadNameList = []
def addThread(self, threadName , targetFunc , haveArgsFlag , args = (1,2)):
if haveArgsFlag == 0:
threadName = threading.Thread(target = targetFunc )
else:
threadName = threading.Thread(target = targetFunc , args = args)
self.threadList.append(threadName)
self.threadNameList.append(threadName)
def delThread(self , threadName):
if threadName in self.threadNameList:
self.threadNameList.remove(threadName)
self.threadList.remove(threadName)
else:
print(threadName + ' not be creat!!!!!!')
def runThread(self):
for t in self.threadList:
t.start()
def stopThread(self):
for t in self.threadList:
t.join()
def forcedStopThread(self):
for t in self.threadList:
self.stop_thread(t)
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:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(self , thread):
self._async_raise(thread.ident, SystemExit)