我创建了一个名为kthread的漂亮包,它简化了python中的线程处理。我还发现,可以通过名称等来命名给定的线程:
import sys
import time
import kthread
def func():
try:
while True:
time.sleep(0.2)
finally:
sys.stdout.write("Greetings from Vice City!n")
sys.stdout.flush()
t = kthread.KThread(target=func, name="KillableThread1")
t.start()
t.is_alive()
t.terminate()
t.is_alive()
不做t.terminate((,我想知道是否有可能删除指定名称的线程?
下面的代码可能会有所帮助。
thread_dict = {}
t1 = kthread.KThread(target=func, name="KillableThread1")
t2 = kthread.KThread(target=func, name="KillableThread2")
thread_dict[t1.name] = t1
thread_dict[t2.name] = t2
之后,当你想杀死一个线程时,请执行以下操作:
thread_dict["KillableThread1"].terminate()