python在一个线程中杀死另外一个线程

python在一个线程中杀死另外一个线程,第1张

概述python在一个线程杀死另外一个线程

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

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 

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的python在一个线程中杀死另外一个线程全部内容,希望文章能够帮你解决python在一个线程中杀死另外一个线程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1198723.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存