总结一下网上关于结束线程的方法

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 强制结束线程相关推荐

  1. python 强制结束线程_在python中实现强制关闭线程的示例

    Python 如何强制关闭线程过去只可以用来回忆,别沉迷在阴影中,否则永远看不清前面的路. Python用sleep停止一个线程的运行,而不影响主线程的运行,案例代码如下: from threadin ...

  2. python如何强制结束主线程_强制结束线程

    场景: 如果某个线程持续阻塞,无法退出,从而导致整个程序无法结束,此时就需要强制结束线程 思路:由于程序阻塞,比如卡在代码中的某一行后,一直无法向下执行,此时,无法通过常规方式结束线程 方法1: 采用 ...

  3. python多线程结束线程_Python线程– Python多线程

    python多线程结束线程 Python threading module is used to implement multithreading in python programs. In thi ...

  4. python多线程结束线程_Python多线程和Office第2部分

    python多线程结束线程 This is the second and final part of the series. You can find the first part of the bl ...

  5. python threading 结束线程

    python threading 启动的线程,并没有提供终止线程的方法,现总结一下在网上找到的方法 1.通过threading.Thread._Thread__stop()结束线程 import ti ...

  6. task.run 强制结束线程_图文介绍进程和线程的区别

    点击蓝色"最码农"关注我哟 加个"星标",每天下午18:03,一起学技术 进程和线程的概念 先了解一下操作系统的一些相关概念,大部分操作系统(如Windows. ...

  7. python强制暂停线程_关于python:使用ThreadPoolExecutor强制线程超时

    我正在升级我的代码以使用ThreadPoolExecuter,并希望能够超时需要超过几秒钟处理的任何线程. 是否可以在作为线程池一部分的线程上强制超时? 我正在使用的代码如下. 1 2 3 4 5 6 ...

  8. python强制结束函数_为什么Python没有 main 函数?终于有人给出了正确答案!

    毫无疑问,Python中没有所谓的 main 入口函数,但在网上经常有文章提到"Python中的main函数"和"建议编写main函数"等. 他们的目的可能是模 ...

  9. python强制暂停线程_如何暂停线程(python)

    上下文: 我正在用Qt creator和python中的"behavior"文件构建一个图形界面.我的GUI的测试版本是: 预期行为: 我正在运行两个不同的线程,它们被引用到同一个 ...

最新文章

  1. 【Git】git 与远程库交互
  2. chrome/edge插件备忘
  3. 网站互点排名掉了网站互点排名掉了网站互点_7天上百度首页排名优化技巧有哪些?...
  4. Java8 Collections.sort()及Arrays.sort()中Lambda表达式及增强版Comparator的使用
  5. 以太网基本知识小结(二)
  6. va_list和vsnprintf、getopt
  7. 使用轻量级Spring @Scheduled注解执行定时任务
  8. 飞鸽传书 再次报道,回到90年代中期的业界
  9. html页面送值转换,在静态页面html中跳转传值
  10. Redhat 7搭建iscsi存储系统
  11. 【python】-- 多进程的基本语法 、进程间数据交互与共享、进程锁和进程池的使用...
  12. python桌面整理小助手
  13. matlab如何泰勒公式用求近似值_泰勒公式在生活中的应用.doc
  14. 为什么要考华为云认证?有什么用?
  15. python f检验 模型拟合度_Python 爬取北京二手房数据,分析北漂族买得起房吗? | 附完整源码...
  16. 复旦非全日制研究生计算机,信息整合!复旦大学非全日制研究生招生详情
  17. 洛谷P1941 飞扬的小鸟
  18. agv系统介绍_AGV物流系统工作流程及模块介绍
  19. c++控制台密码管理系统
  20. Robocup 2D 的学习

热门文章

  1. 国能日新太阳辐射指数预测大赛个人baseline(0.17)
  2. 是德daq970A怎么导出数据
  3. 作为PA AE工程师需要了解的知识
  4. 用php实现抓取电影网站的视频下载地址
  5. 用键盘实现鼠标右键和中键功能
  6. 首尔半导体为美国高端照明品牌供应SunLike产品
  7. STM32学习笔记-LCD中英文字符显示原理
  8. Eclipse工程中java文件上有小问号的原因和解决办法
  9. 牛客小白月赛16 F-小石的妹子(二维偏序+线段树)
  10. Discuz!x2 康盛创想 Windows (server 2003) 服务器部署标准