python 强制结束线程
总结一下网上关于结束线程的方法
1.通过threading.Thread._Thread__stop()结束线程
import time
import threading
def f():while 1:time.sleep(0.1)print(1)t = threading.Thread(target=f)
t.start()
time.sleep(0.5)
print("Stopping the thread")
threading.Thread._Thread__stop(t)
print("Stopped the thread")
这个方法目前用python2能成功结束线程,用python3会报错。一下是分别用python2和python3运行的结果:
python2:
1
1
1
1
Stopping the thread
Stopped the thread
python3:
1
1
1
1
Traceback (most recent call last):File "C:/work/1/reco/12月/tmp.py", line 64, in <module>threading.Thread._Thread__stop(t)
AttributeError: type object 'Thread' has no attribute '_Thread__stop'
Stopping the thread
2.使用ctypes强行杀掉线程
import threading
import time
import inspect
import ctypesdef _async_raise(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")def stop_thread(thread):_async_raise(thread.ident, SystemExit)def print_time(e):while 2:print(e)if __name__ == "__main__":t = threading.Thread(target=print_time,args=("2"))t.start()time.sleep(0.001)stop_thread(t)print("stoped")time.sleep(2)t1 = threading.Thread(target=print_time,args=("1"))t1.start()time.sleep(0.001)stop_thread(t1)print("stoped")while 1:pass
python2和python3都能有效结束线程。
python 强制结束线程相关推荐
- python 强制结束线程_在python中实现强制关闭线程的示例
Python 如何强制关闭线程过去只可以用来回忆,别沉迷在阴影中,否则永远看不清前面的路. Python用sleep停止一个线程的运行,而不影响主线程的运行,案例代码如下: from threadin ...
- python如何强制结束主线程_强制结束线程
场景: 如果某个线程持续阻塞,无法退出,从而导致整个程序无法结束,此时就需要强制结束线程 思路:由于程序阻塞,比如卡在代码中的某一行后,一直无法向下执行,此时,无法通过常规方式结束线程 方法1: 采用 ...
- python多线程结束线程_Python线程– Python多线程
python多线程结束线程 Python threading module is used to implement multithreading in python programs. In thi ...
- python多线程结束线程_Python多线程和Office第2部分
python多线程结束线程 This is the second and final part of the series. You can find the first part of the bl ...
- python threading 结束线程
python threading 启动的线程,并没有提供终止线程的方法,现总结一下在网上找到的方法 1.通过threading.Thread._Thread__stop()结束线程 import ti ...
- task.run 强制结束线程_图文介绍进程和线程的区别
点击蓝色"最码农"关注我哟 加个"星标",每天下午18:03,一起学技术 进程和线程的概念 先了解一下操作系统的一些相关概念,大部分操作系统(如Windows. ...
- python强制暂停线程_关于python:使用ThreadPoolExecutor强制线程超时
我正在升级我的代码以使用ThreadPoolExecuter,并希望能够超时需要超过几秒钟处理的任何线程. 是否可以在作为线程池一部分的线程上强制超时? 我正在使用的代码如下. 1 2 3 4 5 6 ...
- python强制结束函数_为什么Python没有 main 函数?终于有人给出了正确答案!
毫无疑问,Python中没有所谓的 main 入口函数,但在网上经常有文章提到"Python中的main函数"和"建议编写main函数"等. 他们的目的可能是模 ...
- python强制暂停线程_如何暂停线程(python)
上下文: 我正在用Qt creator和python中的"behavior"文件构建一个图形界面.我的GUI的测试版本是: 预期行为: 我正在运行两个不同的线程,它们被引用到同一个 ...
最新文章
- mysql测试工作中的应用_Mysql精华总结,解决测试人员面试中的碰到的一切Mysql问题(一)...
- 九度OJ 1051:数字阶梯求和
- codeforce 227D Naughty Stone Piles (贪心+递归+递推)
- 电脑下载的M4A格式文件怎么转换为MP3格式
- C语言三目运算符 - C语言零基础入门教程
- JSON数据格式---JSON语法规则、JSON文件
- 域名中主机名是第几个_CentOS7系统如何修改主机名
- 微服务升级优点_SpringCloud微服务架构升级总结
- Android: 在WebView中获取网页源码
- XSS的盲打以及实验的演示
- 通过Spring Boot中的手动Bean定义提高启动性能
- [JSOI2010] 连通数
- jrtplib linux编译,linux下编译jrtplib、jthreadlib
- uniapp 修改tabBar图标大小和navigationBar字体大小
- 《AI进化论:解码人工智能商业场景与案例》----读书笔记
- nand flash地址机制
- linux开启PREEMPT_RT
- JavaScript 面试题汇总
- Webpack的基本使用
- snowboy进行语音唤醒,编译snowboy文件