from threading import Thread
import time
class MyThread(Thread):
def __init__(self, stop):
Thread.__init__(self)
self.stop = stop
def run(self):
stop = False
while not stop:
print("I'm running")
time.sleep(1)
# if the signal is stop, break `while loop` so the thread is over.
stop = self.stop
m = MyThread(stop=False)
m.start()
while 1:
i = input("input S to stop\n")
if i == "S":
m.stop = True
break
else:
continue
Threads are an integral part of the process and cannot be killed
outside it. There is the pthread_kill function but it only applies in
the context of the thread itself. From the docs at the link
当然答案是肯定的,我有一个演示代码供参考(不安全):
我不认为可以在进程本身之外终止进程的线程:
如this answer所述
相关问题 更多 >
编程相关推荐