1、通过API进行线程关闭

import threading
import time
import inspect
import ctypes def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
print(tid,exctype)
tid = ctypes.c_long(tid)
print tid
if not inspect.isclass(exctype):
print exctype
exctype = type(exctype)
print exctype
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
print res
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:
# print 'run'
time.sleep(0.1)
print "end"
if __name__ == "__main__":
t = TestThread()
t.start()
time.sleep(1)
stop_thread(t)
print "stoped"

注:杀不起正在阻塞的线程

  

Python开发【笔记】:关闭线程的方法的更多相关文章

  1. 【python学习笔记】9.魔法方法、属性和迭代器

    [python学习笔记]9.魔法方法.属性和迭代器 魔法方法:xx, 收尾各有两个下划线的方法 __init__(self): 构造方法,创建对象时候自动执行,可以为其增加参数, 父类构造方法不会被自 ...

  2. python开发笔记-通过xml快捷获取数据

    今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...

  3. python学习笔记12 ----线程、进程

    进程和线程的概念 进程和线程是操作系统中两个很重要的概念,对于一般的程序,可能有若干个进程,每一个进程有若干个同时执行的线程.进程是资源管理的最小单位,线程是程序执行的最小单位(线程可共享同一进程里的 ...

  4. python学习笔记11 ----线程、进程、协程

    进程.线程.协程的概念 进程和线程是操作系统中两个很重要的概念,对于一般的程序,可能有若干个进程,每一个进程有若干个同时执行的线程.进程是资源管理的最小单位,线程是程序执行的最小单位(线程可共享同一进 ...

  5. WP开发笔记——程序的退出方法

    Windows Phone程序中,并没有之前的类似于“App.Exit()”之类的函数用来让你退出程序.这是怎么回事儿呢? 很简单,在Windows Phone 7中系统要求配备了硬件的“Back”键 ...

  6. Java 学习笔记之 线程isInterrupted方法

    线程isInterrupted方法: isInterrupted()是Thread对象的方法,测试线程是否已经中断. public class ThreadRunMain { public stati ...

  7. Java 学习笔记之 线程interrupted方法

    线程interrupted方法: interrupted()是Thread类的方法,用来测试当前线程是否已经中断. public class InterruptThread extends Threa ...

  8. Java 学习笔记之 线程interrupt方法

    线程interrupt方法: interrupt方法是用来停止线程的,但是他的使用效果并不像for+break那样,马上就停止循环. 调用interrupt()其实仅仅是在当前线程中打了一个停止标记, ...

  9. Java 学习笔记之 线程sleep方法

    线程sleep方法: 单主线程使用sleep: Main线程差了2000毫秒. public class MainSleepThread extends Thread{ @Override publi ...

  10. python开发笔记-python调用webservice接口

    环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...

随机推荐

  1. MongoDB(三)-- 执行JS、界面工具

    一.执行Js脚本 1.开启mongod服务 2.连接mongodb客户端,./mongo --host 192.168.80.128 --port 27017 3.创建数据库:use testdb1 ...

  2. Django 创建第一个项目

    创建项目: [root@localhost ~]$ django-admin.py startproject web # web是项目名 [root@localhost ~]$ tree web/ w ...

  3. 3dmax导出模型使用相对路径读取纹理贴图

    Shift+T快捷键打开“资源跟踪”窗口

  4. C++标准程序库笔记之一

    本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...

  5. 【技术分享会】 @第四期 JQuery插件

    本讲内容 JavaScript JQuery JQuery插件 实例 JavaScript 前端开发工程师必须掌握的三种技能 描述内容的HTML 描述网页样式的CSS 描述网页行为的JavaScrip ...

  6. 游戏服务器学习笔记 5———— twisted Perspective Broker 透明代理

    实际上这章压根不需要我来说,twisted官网的Doc里面有专门介绍的章节.写的非常详细. http://twistedmatrix.com/documents/current/core/howto/ ...

  7. scanf printf gets() puts(),cin cout

    最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...

  8. String.Split()函数 多种使用实例

    我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),其中用到了String.SPl ...

  9. java(6) ArrayList源码

    系统环境: JDK 1.7 成员变量 //默认的初始化数组大小 private static final int DEFAULT_CAPACITY = 10; //空的对象数组 private sta ...

  10. sencha touch list + carousel scrollable(与其他控件共用滚动条)

    有些时候我们需要实现这种效果 上边是一张图片或者一个跑马灯控件,这个布局实现起来比较容易 但是如何让他们共用一个滚动条,来实现以下效果就比较麻烦了. 在官方论坛查找资料得知,可以用以下写法实现: /* ...