在python中实现强制关闭线程的示例


Posted in Python onJanuary 22, 2019

如下所示:

import threading
import time
import inspect
import ctypes


def _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)


class TestThread(threading.Thread):
  def run(self):
   print
   "begin"
   while True:
     time.sleep(0.1)
   print('end')


if __name__ == "__main__":
  t = TestThread()
  t.start()
  time.sleep(1)
  stop_thread(t)
  print('stoped')

以上这篇在python中实现强制关闭线程的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
自己编程中遇到的Python错误和解决方法汇总整理
Jun 03 Python
python用户评论标签匹配的解决方法
May 31 Python
pandas表连接 索引上的合并方法
Jun 08 Python
Python中常用的8种字符串操作方法
May 06 Python
Python基本数据结构之字典类型dict用法分析
Jun 08 Python
详解python uiautomator2 watcher的使用方法
Sep 09 Python
Python如何使用BeautifulSoup爬取网页信息
Nov 26 Python
对tensorflow中的strides参数使用详解
Jan 04 Python
python实现在内存中读写str和二进制数据代码
Apr 24 Python
opencv+python实现鼠标点击图像,输出该点的RGB和HSV值
Jun 02 Python
Python 的 __str__ 和 __repr__ 方法对比
Sep 02 Python
Python的logging模块基本用法
Dec 24 Python
Python实现简单石头剪刀布游戏
Jan 20 #Python
python石头剪刀布小游戏(三局两胜制)
Jan 20 #Python
python 对类的成员函数开启线程的方法
Jan 22 #Python
python实现石头剪刀布小游戏
Jan 20 #Python
对Python3之进程池与回调函数的实例详解
Jan 22 #Python
python多任务及返回值的处理方法
Jan 22 #Python
opencv实现静态手势识别 opencv实现剪刀石头布游戏
Jan 22 #Python
You might like
站长助手-网站web在线管理程序 v1.0 下载
2007/05/12 PHP
PHP中auto_prepend_file与auto_append_file用法实例分析
2014/09/22 PHP
PHP并发多进程处理利器Gearman使用介绍
2016/05/16 PHP
setTimeout和setInterval的浏览器兼容性分析
2007/02/27 Javascript
javascript算法学习(直接插入排序)
2011/04/12 Javascript
深入分析js的冒泡事件
2014/12/05 Javascript
node.js中的emitter.on方法使用说明
2014/12/10 Javascript
基于jQuery 实现bootstrapValidator下的全局验证
2015/12/07 Javascript
修改js confirm alert 提示框文字的简单实例
2016/06/10 Javascript
Three.js学习之几何形状
2016/08/01 Javascript
Vuejs第七篇之Vuejs过渡动画案例全面解析
2016/09/05 Javascript
JS前端笔试题分析
2016/12/19 Javascript
微信小程序商城项目之商品属性分类(4)
2017/04/17 Javascript
vue 实现的树形菜的实例代码
2018/03/19 Javascript
vue中rem的配置的方法示例
2018/08/30 Javascript
javascript实现画板功能
2020/04/12 Javascript
Python中多线程thread与threading的实现方法
2014/08/18 Python
Python实现的三层BP神经网络算法示例
2018/02/07 Python
python抽取指定url页面的title方法
2018/05/11 Python
Python爬虫框架Scrapy常用命令总结
2018/07/26 Python
解决python3中的requests解析中文页面出现乱码问题
2019/04/19 Python
python开启debug模式的方法
2019/06/27 Python
python实现socket+threading处理多连接的方法
2019/07/23 Python
详解用Python为直方图绘制拟合曲线的两种方法
2019/08/21 Python
用python计算文件的MD5值
2020/12/23 Python
html5图片上传预览示例分享
2014/04/14 HTML / CSS
秋季运动会通讯稿
2014/01/24 职场文书
临床护士自荐信
2014/01/31 职场文书
宣传策划类求职信范文
2014/01/31 职场文书
机关保密承诺书
2014/06/03 职场文书
新文化运动的口号
2014/06/21 职场文书
大学社团招新的通讯稿
2014/09/10 职场文书
共青团员自我评价范文
2014/09/14 职场文书
群众路线个人对照检查材料
2014/09/23 职场文书
2015年药店店长工作总结
2015/04/29 职场文书
 python中的元类metaclass详情
2022/05/30 Python