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. Python基础(1)——变量和数据类型[xiaoshun]

    目录 一.变量 1.概述 Variables are used to store information to be referenced(引用)and manipulated(操作) in a co ...

  2. Kubernetes 实战 —— 03. pod: 运行于 Kubernetes 中的容器

    介绍 pod P53 pod 是 Kubernetes 中最为重要的核心概念,而其他对象仅仅用于 pod 管理. pod 暴露或被 pod 使用. pod 是一组并置的容器,代表了 Kubernete ...

  3. 【图像处理】OpenCV+Python图像处理入门教程(五)阈值处理

    这篇随笔介绍使用OpenCV进行图像处理的第五章 阈值处理. 5  阈值处理 阈值是指像素到达某临界值.阈值处理表示像素到达某临界值后,对该像素点进行操作和处理. 例如:设定一幅图像素阈值为200,则 ...

  4. A New Stone Game POJ - 1740

    题目链接:https://vjudge.net/problem/POJ-1740#author=0 题意:有n堆石子,每次你可以选一堆拿走任意数量的石子,而且你还可以选择从这一堆剩下石子中取任意数量石 ...

  5. Hznu_0j 1557

    题目链接:http://acm.hznu.edu.cn/OJ/problem.php?id=1557 题解:将两个数组分别升序和降序排序后,累加差的绝对值. Ac代码: #include<std ...

  6. 关于Handler同步屏障你可能不知道的问题

    前言 很高兴遇见你 ~ 关于handler的内容,基本每个android开发者都掌握了,网络中的优秀博客也非常多,我之前也写过一篇文章,读者感兴趣可以去看看:传送门. 这篇文章主要讲Handler中的 ...

  7. 攻防世界 reverse 666

    666  2019_UNCTF main int __cdecl main(int argc, const char **argv, const char **envp) { char myen; / ...

  8. 攻防世界 reverse 进阶 1-4

    1.dmd-50 suctf-2016 md5后比较,在线解md5得到: md5(md5($pass)),所以将grape再进行MD5 b781cbb29054db12f88f08c6e161c199 ...

  9. ASPOSE.Cells & ASPOSE.Words 操纵Excel和Word文档的 .NET Core 实例

    Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,它提供的原生API可以对Word.Excel.PDF.Powerpoint.Outlook.CAD.图片.3D.ZI ...

  10. 教你如何用Python模拟http请求(GET,POST)

    模拟http请求有什么用呢? 我们现在使用的所有需要使用网络的:软件 应用 app 网站里面的绝大部分功能都是通过http协议来工作的 什么是http协议? http协议,超文本传输协议(HTTP,H ...