• <del id="a8uas"></del>
    • 千鋒教育-做有情懷、有良心、有品質的職業教育機構

      400-811-9990
      手機站
      千鋒教育

      千鋒學習站 | 隨時隨地免費學

      千鋒教育

      掃一掃進入千鋒手機站

      領取全套視頻
      千鋒教育

      關注千鋒學習站小程序
      隨時隨地免費學習課程

      上海
      • 北京
      • 鄭州
      • 武漢
      • 成都
      • 西安
      • 沈陽
      • 廣州
      • 南京
      • 深圳
      • 大連
      • 青島
      • 杭州
      • 重慶
      當前位置:合肥千鋒IT培訓  >  技術干貨  >  python怎樣終止線程

      python怎樣終止線程

      來源:千鋒教育
      發布人:xqq
      時間: 2023-11-06 04:37:00

      python中啟動和關閉線程:

      一、啟動線程

      首先導入threading

      importthreading

      然后定義一個方法

      defserial_read():

      ...

      ...

      然后定義線程,target指向要執行的方法

      myThread=threading.Thread(target=serial_read)

      啟動它

      myThread.start()

      二、停止線程

      importinspect

      importctypes

      def_async_raise(tid,exctype):

      """raisestheexception,performscleanupifneeded"""

      tid=ctypes.c_long(tid)

      ifnotinspect.isclass(exctype):

      exctype=type(exctype)

      res=ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,ctypes.py_object(exctype))

      ifres==0:

      raiseValueError("invalidthreadid")

      elifres!=1:

      #"""ifitreturnsanumbergreaterthanone,you'reintrouble,

      #andyoushouldcallitagainwithexc=NULLtoreverttheeffect"""

      ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,None)

      raiseSystemError("PyThreadState_SetAsyncExcfailed")

      defstop_thread(thread):

      _async_raise(thread.ident,SystemExit)

      停止線程

      stop_thread(myThread)

      stop方法可以強行終止正在運行或掛起的線程。

      以上內容為大家介紹了python怎樣終止線程,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。

      聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。

      猜你喜歡LIKE

      python解釋器的多種使用

      2023-11-06

      python縮進和空格的好處

      2023-11-06

      python變量不需要聲明嗎

      2023-11-06

      最新文章NEW

      python海象運算符的使用

      2023-11-06

      python division如何取整

      2023-11-06

      pythonsort()報錯是什么原因

      2023-11-06

      相關推薦HOT

      更多>>

      快速通道 更多>>

      最新開班信息 更多>>

      網友熱搜 更多>>