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

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

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

如下所示:

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中實現(xiàn)強制關(guān)閉線程的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決nohup重定向python輸出到文件不成功的問題

    解決nohup重定向python輸出到文件不成功的問題

    今天小編就為大家分享一篇解決nohup重定向python輸出到文件不成功的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python基于回溯法子集樹模板實現(xiàn)圖的遍歷功能示例

    Python基于回溯法子集樹模板實現(xiàn)圖的遍歷功能示例

    這篇文章主要介紹了Python基于回溯法子集樹模板實現(xiàn)圖的遍歷功能,結(jié)合實例形式分析了Python使用回溯法子集樹模板針對圖形遍歷問題的相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2017-09-09
  • PyQt5每天必學(xué)之帶有標簽的復(fù)選框

    PyQt5每天必學(xué)之帶有標簽的復(fù)選框

    這篇文章主要為大家詳細介紹了PyQt5每天必學(xué)之復(fù)選框的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 最新評論