python杀死一个线程的方法

 更新时间:2015年09月06日 09:39:30   投稿:mrr  
由于python线程没有提供abort方法,所以我们需要自己想办法解决此问题,面对这一问题,小编帮大家解决phthon杀死一个线程的方法,需要的朋友一起来学习吧

最近在项目中遇到这一需求:

我需要一个函数工作,比如远程连接一个端口,远程读取文件等,但是我给的时间有限,比如,4秒钟如果你还没有读取完成或者连接成功,我就不等了,很可能对方已经宕机或者拒绝了。这样可以批量做一些事情而不需要一直等,浪费时间。

结合我的需求,我想到这种办法:

1、在主进程执行,调用一个进程执行函数,然后主进程sleep,等时间到了,就kill 执行函数的进程。

测试一个例子:

import time 
import threading 
def p(i): 
  print i 
class task(threading.Thread): 
  def __init__(self,fun,i): 
    threading.Thread.__init__(self) 
    self.fun = fun 
    self.i = i 
    self.thread_stop = False 
  def run(self): 
    while not self.thread_stop: 
      self.fun(self.i) 
  def stop(self): 
    self.thread_stop = True 
def test(): 
  thread1 = task(p,2) 
  thread1.start() 
  time.sleep(4) 
  thread1.stop() 
  return 
if __name__ == '__main__': 
  test()

经过测试只定了4秒钟。

经过我的一番折腾,想到了join函数,这个函数式用来等待一个线程结束的,如果这个函数没有结束的话,那么,就会阻塞当前运行的程序。关键是,这个参数有一个可选参数:join([timeout]):  阻塞当前上下文环境的线程,直到调用此方法的线程终止或到达指定的timeout(可选参数)。

不多说了贴下面代码大家看下:

#!/usr/bin/env python 
#-*-coding:utf-8-*- 
''''' 
author:cogbee 
time:2014-6-13 
function:readme 
''' 
import pdb 
import time 
import threading 
import os 
#pdb.set_trace() 
class task(threading.Thread): 
  def __init__(self,ip): 
    threading.Thread.__init__(self) 
    self.ip = ip 
    self.thread_stop = False 
  def run(self): 
    while not self.thread_stop:   
      #//添加你要做的事情,如果成功了就设置一下<span style="font-family: Arial, Helvetica, sans-serif;">self.thread_stop变量。</span> 
[python] view plaincopy在CODE上查看代码片派生到我的代码片
      if file != '': 
        self.thread_stop = True 
  def stop(self): 
    self.thread_stop = True 
def test(eachline): 
  global file 
  list = [] 
  for ip in eachline: 
    thread1 = task(ip) 
    thread1.start() 
    thread1.join(3) 
    if thread1.isAlive():   
      thread1.stop() 
      continue 
    #将可以读取的都存起来 
    if file != '': 
      list.append(ip) 
  print list 
if __name__ == '__main__': 
  eachline = ['1.1.1.1','222.73.5.54'] 
  test(eachline)

下面给大家分享我写的一段杀死线程的代码。

由于python线程没有提供abort方法,分享下面一段代码杀死线程:

import threading 
import inspect 
import ctypes 
def _async_raise(tid, exctype):
  """raises the exception, performs cleanup if needed"""
  if not inspect.isclass(exctype):
    raise TypeError("Only types can be raised (not instances)")
  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, 0)
    raise SystemError("PyThreadState_SetAsyncExc failed")
class Thread(threading.Thread):
  def _get_my_tid(self):
    """determines this (self's) thread id"""
    if not self.isAlive():
      raise threading.ThreadError("the thread is not active")
    # do we have it cached?
    if hasattr(self, "_thread_id"):
      return self._thread_id
    # no, look for it in the _active dict
    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):
    """raises the given exception type in the context of this thread"""
    _async_raise(self._get_my_tid(), exctype)
def terminate(self):
    """raises SystemExit in the context of the given thread, which should 
    cause the thread to exit silently (unless caught)"""
    self.raise_exc(SystemExit)

使用例子:

>>> import time 
>>> from thread2 import Thread 
>>> 
>>> def f(): 
...   try: 
...     while True: 
...       time.sleep(0.1) 
...   finally: 
...     print "outta here" 
... 
>>> t = Thread(target = f) 
>>> t.start() 
>>> t.isAlive() 
True 
>>> t.terminate() 
>>> t.join() 
outta here 
>>> t.isAlive() 
False

试了一下,很不错,只是在要kill的线程中如果有time.sleep()时,好像工作不正常,没有找出真正的原因是什么。已经是很强大了。哈哈。

相关文章

  • python中的异步爬虫详解

    python中的异步爬虫详解

    这篇文章主要介绍了python中的异步爬虫详解,所谓的异步异步 IO,就是发起一个 IO 阻塞的操作,但是不用等到它结束,可以在它执行 IO 的过程中继续做别的事情,当 IO 执行完毕之后会收到它的通知,需要的朋友可以参考下
    2023-08-08
  • Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法示例

    Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法示例

    这篇文章主要介绍了Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法,结合实例形式分析了Python使用pymysql模块的fetchone(), fetchmany(), fetchall()方法进行mysql数据库查询的操作技巧,需要的朋友可以参考下
    2019-10-10
  • Python进阶多线程爬取网页项目实战

    Python进阶多线程爬取网页项目实战

    这篇文章主要为大家介绍了Python进阶,Python多线程爬取网页项目实战的示例呈现步骤,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-10-10
  • matplotlib bar()实现百分比堆积柱状图

    matplotlib bar()实现百分比堆积柱状图

    这篇文章主要介绍了matplotlib bar()实现百分比堆积柱状图,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python中多个数组行合并及列合并的方法总结

    Python中多个数组行合并及列合并的方法总结

    下面小编就为大家分享一篇Python中多个数组行合并及列合并的方法总结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • python argparse模块传参用法实例

    python argparse模块传参用法实例

    这篇文章主要为大家介绍了python argparse模块传参用法实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • python数组中的 k-diff 数对例题解析

    python数组中的 k-diff 数对例题解析

    这篇文章主要介绍了python数组中的 k-diff 数对例题解析,文章根据题目内容对其进行分析以此展开主题内容,感兴趣的小伙伴可以参考一下下面文章详情
    2022-06-06
  • Python的动态重新封装的教程

    Python的动态重新封装的教程

    这篇文章主要介绍了Python的动态重新封装的教程,本文来自于IBM的官方开发者文档,需要的朋友可以参考下
    2015-04-04
  • 详解Python3 对象组合zip()和回退方式*zip

    详解Python3 对象组合zip()和回退方式*zip

    这篇文章主要介绍了Python3 对象组合zip()和回退方式*zip详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • python基于Tkinter实现人员管理系统

    python基于Tkinter实现人员管理系统

    这篇文章主要为大家详细介绍了python基于Tkinter实现人员管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11

最新评论