欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在python中實(shí)現(xiàn)強(qiáng)制關(guān)閉線程的示例

 更新時(shí)間:2019年01月22日 15:24:44   作者:tian544556  
今天小編就為大家分享一篇在python中實(shí)現(xiàn)強(qiáng)制關(guān)閉線程的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

import threading
import time
import inspect
import ctypes


def _async_raise(tid, exctype):
  """raises the exception, performs cleanup if needed"""
  tid = ctypes.c_long(tid)
  if not inspect.isclass(exctype):
   exctype = type(exctype)
  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, 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:
     time.sleep(0.1)
   print('end')


if __name__ == "__main__":
  t = TestThread()
  t.start()
  time.sleep(1)
  stop_thread(t)
  print('stoped') 

以上這篇在python中實(shí)現(xiàn)強(qiáng)制關(guān)閉線程的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論