在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 16 Python
教你如何在Django 1.6中正确使用 Signal
Jun 22 Python
Python的动态重新封装的教程
Apr 11 Python
Python的Django框架中消息通知的计数器实现教程
Jun 13 Python
python 连接各类主流数据库的实例代码
Jan 30 Python
在windows下Python打印彩色字体的方法
May 15 Python
Django uwsgi Nginx 的生产环境部署详解
Feb 02 Python
Python实现捕获异常发生的文件和具体行数
Apr 25 Python
从0到1使用python开发一个半自动答题小程序的实现
May 12 Python
Python Dict找出value大于某值或key大于某值的所有项方式
Jun 05 Python
深入理解Python 多线程
Jun 16 Python
基于Python实现体育彩票选号器功能代码实例
Sep 16 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
在php和MySql中计算时间差的方法
2011/04/22 PHP
PHP里的单例类写法实例
2015/06/25 PHP
php的socket编程详解
2016/11/20 PHP
php数据库的增删改查 php与javascript之间的交互
2017/08/31 PHP
PHP面向对象程序设计之对象的遍历操作示例
2019/06/12 PHP
javascript 显示当前系统时间代码
2009/12/28 Javascript
Eval and new funciton not the same thing
2012/12/27 Javascript
jQuery插件 selectToSelect使用方法
2013/10/02 Javascript
jquery实现背景墙聚光灯效果示例分享
2014/03/02 Javascript
用json方式实现在 js 中建立一个map
2014/05/02 Javascript
Bootstrap模块dropdown实现下拉框响应
2016/05/22 Javascript
Jquery和Js获得元素标签名称的方法总结
2016/10/08 Javascript
js仿新浪微博消息发布功能
2017/02/17 Javascript
Django1.7+JQuery+Ajax验证用户注册集成小例子
2017/04/08 jQuery
bootstrap 点击空白处popover弹出框隐藏实例
2018/01/24 Javascript
基于$.ajax()方法从服务器获取json数据的几种方式总结
2018/01/31 Javascript
小程序ios音频播放没声音问题的解决
2018/07/11 Javascript
Python遍历目录中的所有文件的方法
2016/07/08 Python
python输入错误密码用户锁定实现方法
2017/11/27 Python
Python的SimpleHTTPServer模块用处及使用方法简介
2018/01/22 Python
通过Pandas读取大文件的实例
2018/06/07 Python
python计算两个地址之间的距离方法
2018/06/09 Python
快速解决pandas.read_csv()乱码的问题
2018/06/15 Python
python实现录音小程序
2020/10/26 Python
python print输出延时,让其立刻输出的方法
2019/01/07 Python
用Python徒手撸一个股票回测框架搭建【推荐】
2019/08/05 Python
如何在python开发工具PyCharm中搭建QtPy环境(教程详解)
2020/02/04 Python
matlab灰度图像调整及imadjust函数的用法详解
2020/02/27 Python
Python如何将函数值赋给变量
2020/04/28 Python
python实现一次性封装多条sql语句(begin end)
2020/06/06 Python
通过实例解析python创建进程常用方法
2020/06/19 Python
浅谈Python里面None True False之间的区别
2020/07/09 Python
孟佩杰观后感
2015/06/17 职场文书
MySQL中B树索引和B+树索引的区别详解
2022/03/03 MySQL
教你win10系统中APPCRASH事件问题解决方法
2022/07/15 数码科技
httpclient调用远程接口的方法
2022/08/14 Java/Android