日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python3 停止线程_python3怎么关闭线程

發(fā)布時(shí)間:2024/9/19 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 停止线程_python3怎么关闭线程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python3利用自定義異常來(lái)退出并關(guān)閉線程。方法:1、利用raise自定義異常;2、當(dāng)觸發(fā)函數(shù)stop_thread時(shí)調(diào)用自定義異常進(jìn)行退出。

利用異常使線程退出代碼如下: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)

停止線程stop_thread(myThread)

補(bǔ)充知識(shí):python threading實(shí)現(xiàn)Thread的修改值,開(kāi)始,運(yùn)行,停止,并獲得內(nèi)部值

總結(jié)

以上是生活随笔為你收集整理的python3 停止线程_python3怎么关闭线程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。