python没有为内置的threading.Thread类提供一个kill接口,可以通过使用CPython API向线程抛出一个SystemExit异常来终止线程。如果线程没有被系统调用阻塞(sleep, recv, accept等),线程将会强制退出。参考连接:https://github.com/munshigroup/kthread.

import ctypes
import inspect try:
import thread
except ImportError:
import _thread as thread
import threading name = "kthread" def _async_raise(tid, exctype):
if not inspect.isclass(exctype):
raise TypeError("Only types can be raised (not instances)")
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
if res == 0:
raise ValueError("Invalid thread ID")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
raise SystemError("PyThreadState_SetAsyncExc failed") class KThread(threading.Thread):
def _get_my_tid(self):
if not self.isAlive():
raise threading.ThreadError("Thread is not active") if hasattr(self, "_thread_id"):
return self._thread_id for tid, tobj in threading._active.items():
if tobj is self:
self._thread_id = tid
return tid raise AssertionError("Could not determine the thread's ID") def raise_exc(self, exctype):
_async_raise(self._get_my_tid(), exctype) def terminate(self):
"""cause the thread to exit silently (unless caught)"""
# terminate会被系统调用(recv, sleep, accept等)阻塞
self.raise_exc(SystemExit) def kill(self):
self.terminate() def exit(self):
self.terminate()

killable thread的python实现的更多相关文章

  1. thread/threading——Python多线程入门笔记

    1 什么是线程? (1)线程不同于程序. 线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制: 多线程类似于同时执行多个不同程序. (2)线程不同于进程. 每个独立的进程有一个程 ...

  2. thread模块—Python多线程编程

    Thread 模块 *注:在实际使用过程中不建议使用 thread 进行多线程编程,本文档只为学习(或熟悉)多线程使用. Thread 模块除了派生线程外,还提供了基本的同步数据结构,称为锁对象(lo ...

  3. python multi process multi thread

    muti thread: python threading: https://docs.python.org/2/library/threading.html#thread-objects https ...

  4. 多线程在python中的使用 thread

    近期想学习研究一下python中使用多线程,来提高python在爬虫项目中的效率. 如今我们在网页上查询到在python中使用的多线程的使用大多数都是使用的threading模块,可是python中另 ...

  5. python从写定时器学习Thread

    目录 python从写定时器学习Thread Timer 对象 粗陋的循环定时器 更 pythonic 循环定时器 FAQ python从写定时器学习Thread python 如何写一个定时器,循环 ...

  6. 【python】python新手必碰到的问题---encode与decode,中文乱码[转]

    转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...

  7. Python多线程(1)——介绍

    Python对多线程提供了很好的支持,Python中多线程相关的模块包括:thread,threading,Queue.可以方便地支持创建线程.互斥锁.信号量.同步等特性. 1. thread:多线程 ...

  8. Python:使用threading模块实现多线程编程

    转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...

  9. 进程,线程,GIL,Python多线程,生产者消费者模型都是什么鬼

    1. 操作系统基本知识,进程,线程 CPU是计算机的核心,承担了所有的计算任务: 操作系统是计算机的管理者,它负责任务的调度.资源的分配和管理,统领整个计算机硬件:那么操作系统是如何进行任务调度的呢? ...

随机推荐

  1. Hibernate 组合查询+分页

    MVC模型:Hibernate+Struts2 dao层: public List<UserBean> searchList(UserBean uBean,int pageIndex,in ...

  2. Linux less命令查看文件常用查询方法

    g 跳到文件开头 G 跳到文件结尾 / 往下搜索字符 ? 网上搜索字符 n 执行上一个搜索(/或者?的搜索),例如上一个搜索是使用/搜索的,则继续使用/搜索,即往下搜索结果 N 反向执行上一个搜索(/ ...

  3. 为什么是InfluxDB | 写在《InfluxDB原理和实战》出版之际

    1年前写的一篇旧文,文中的分析,以及探讨的问题和观点,至今仍有意义. 从2016年起,笔者在腾讯公司负责QQ后台的海量服务分布式组件的架构设计和研发工作,例如微服务开发框架SPP.名字路由CMLB.名 ...

  4. The League of Sequence Designers Gym - 102460E

    题目链接:https://vjudge.net/problem/Gym-102460E 思路:求: 题目当中给了一段伪代码算法,仔细一看发现它是不会记录负数情况,所以与正确答案会有误差,现在题目给定K ...

  5. IDA 创建本地类型

    在IDA中我们常常使用 shift+F9打开结构体视图,ins 创建结构体,但操作有些繁琐. 我们可以在View-->Open Subviews-->Local Types(视图--> ...

  6. 在PHP7以上版本使用不了mysql扩展

    旧程序使用了mysql扩展,而新环境却是PHP7以上版本,不支持mysql扩展,办法是将旧程序中的mysql相关内容修改为mysqli或PDO代码. 但是涉及修改的量大,那则可以包含(include ...

  7. ubuntu20.04开机显示recovering journal死机的解决方法

    事发突然,在今天开机的时候无法进入登陆界面,一直卡在黑屏界面,屏幕上只显示几行代码,且任何按键都无法起作用 /dev/sdb2:recovering journal /dev/sdb2:Clearin ...

  8. 由一名保安引发的Java设计模式:外观模式

    目录 应用场景 外观模式 定义 意图 主要解决问题 何时使用 优缺点 结构 保安的故事 应用场景 使用方要完成一个功能,需要调用提供方的多个接口.方法,调用过程复杂时,我们可以再提供一个高层接口(新的 ...

  9. Parentheses Balance UVA - 673

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  10. Throwing cards away I UVA - 10935

      Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the botto ...