import threading
import time
import inspect
import ctypes
def stop_thread(thread, exctype=SystemExit):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(thread.ident)
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")
def print_time(s):
while True:
print(str(s)+'-------111111111111')
a=0
while True:
a+=1
print(str(s) + "-------" + str(a))
if a==10:
break
time.sleep(0.5)
time.sleep(1)
t_list=[]
if __name__ == "__main__":
while True:
a=input('输入一个数')
t = threading.Thread(target=print_time,args=(str(int(time.time()))[-3:],))
if a=='1':
t.start()
t_list.append(t)
if a=='2':
print(t_list)
for i in t_list:
stop_thread(i)
print("stoped")
t_list=[]
python 强行杀死子线程例子
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- okHttp用于android的http请求。据说很厉害,我们来一起尝尝鲜。但是使用okHttp也会有一些小坑,后...
- 着重讲下daemon这个参数,他是python3中Thread才有的参数。虽然他可以让线程后台继续运行,但是如果主...
- 写在之前: 一、为什么写这往篇文章 正巧遇到这样一个问题:在linux有一个服务,在需要用的时候,再开启;但开启后...
- 今天想实现多线程更新资产信息,所以使用到了threading,但是我需要每个线程的返回值,这就需要我在thread...