黑松山资源网 Design By www.paidiu.com
最近在项目中遇到这一需求:
我需要一个函数工作,比如远程连接一个端口,远程读取文件等,但是我给的时间有限,比如,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"_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()时,好像工作不正常,没有找出真正的原因是什么。已经是很强大了。哈哈。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 徐璨宾《井底的蚯蚓》[320K/MP3][78.77MB]
- 碧蓝航线国庆金秋版本重磅上线 新玩法3D宿舍系统闪亮登场
- 自在西游自在服即将在10月1日开启 入驻即享豪礼满满
- 炉石传说最强术士卡组是哪个 术士最强天梯卡组推荐一览
- 炉石传说最强萨尔卡组是哪个 萨尔最强天梯卡组推荐一览
- 炉石传说最强潜行者卡组是哪个 潜行者最强天梯卡组推荐一览
- 银霞.1983-《爱迷惑我·我住小楼中》台湾复刻版[WAV+CUE]
- [雨果唱片]粤曲名家-《再折长亭柳》[WAV+CUE]
- 刘德华1998-你是我的女人[香港第二版][WAV]
- 压迫感谁最强?外媒评选恐怖游戏最佳女反派
- 还没正式发售:《寂静岭2:重制版》Steam玩家峰值近9000人
- 别搞政确了!超95%国外玩家反对强制包容性
- 《暗喻幻想:ReFantazio》试玩:ATLUS的自我挑战?
- 《憧憬成为魔法少女》第二季 反转魔法少女题材再掀热潮
- 海信AI电视E7N正式发布,引领AI画质新标杆