婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Python的子線程和子進(jìn)程是如何手動結(jié)束的?

Python的子線程和子進(jìn)程是如何手動結(jié)束的?

熱門標(biāo)簽:如何地圖標(biāo)注公司 外賣地址有什么地圖標(biāo)注 預(yù)覽式外呼系統(tǒng) 煙臺電話外呼營銷系統(tǒng) 上海正規(guī)的外呼系統(tǒng)最新報價 企業(yè)彩鈴地圖標(biāo)注 銀川電話機(jī)器人電話 長春極信防封電銷卡批發(fā) 電銷機(jī)器人錄音要學(xué)習(xí)什么

如何結(jié)束Python的子線程和子進(jìn)程

結(jié)束子線程的方法:

這個是搬運(yùn)其他大神的代碼,鄙人也不知道原理,反正拿來主義,暫時沒發(fā)現(xiàn)什么缺點,先用著再說。

import inspect
import ctypes
import threading
from time import sleep
 
 
def serial_read():
 
    while True:
 
        print("春哥純爺們!")
 
        sleep(1)
 
 
 
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)
 
 
def Air():
 
    ords=0
 
    myThread = threading.Thread(target=serial_read)
 
    myThread.start()
 
    while True:
 
 
        ords+=1
 
        if ords==10:
 
            stop_thread(myThread)
            print("停止子線程")
            break
 
 
        sleep(1)
 
 
if __name__ == '__main__':
 
    Air()

下面是結(jié)束子進(jìn)程的方法:

import inspect
import ctypes
from time import sleep
from multiprocessing import Process
 
 
def serial_read():
 
    while True:
 
        print("春哥純爺們!")
 
        sleep(1)
 
 
def Air():
 
    ords=0
 
    myThread = Process(target=serial_read)
 
    myThread.start()
 
    while True:
 
 
        ords+=1
 
        if ords==10:
 
            myThread.terminate()
            print("停止子進(jìn)程")
            break
 
 
        sleep(1)
 
 
if __name__ == '__main__':
 
    Air()

這里說一下如果用類的話要如何寫,想結(jié)束子進(jìn)程或者子線程就需要拿到進(jìn)程對象或者線程對象,但是類中沒辦法實現(xiàn)創(chuàng)建一個類屬性的方式然后用self.×××的方式來在其他類方法中調(diào)用,這時候就創(chuàng)建一個類屬性list,然后創(chuàng)建好子進(jìn)程或者子線程后把對象賦值給這個list,再在類的其他方法中調(diào)用這個list中的元素,就拿到了子進(jìn)程或者子線程的對象了。

例如:

def startss(self,a1,b1,c1,under,rough,blue,among):
 
        # 創(chuàng)建新線程
        p1=threading.Thread(target=self.line01,args=(a1,b1,under,rough,)) #必須加,號 
        p2=threading.Thread(target=self.line02,args=(a1,c1,under,blue,))
        p3=Process(target=self.Process01,args=(under,rough,blue,)) #計算進(jìn)程
 
        #among是類屬性list容器
 
        among.append(p1)
        among.append(p2)
        among.append(p3)
 
        # 開啟新線程
        p1.start()
        p2.start()
 
        #開啟計算用進(jìn)程
        p3.start()

參考資料:https://www.jb51.net/article/185867.htm

python 多進(jìn)程如何終止或重啟子進(jìn)程

到此這篇關(guān)于Python的子線程和子進(jìn)程是如何手動結(jié)束的?的文章就介紹到這了,更多相關(guān)Python的子線程和子進(jìn)程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python子線程如何有序執(zhí)行
  • 解決python父線程關(guān)閉后子線程不關(guān)閉問題
  • Python 多線程,threading模塊,創(chuàng)建子線程的兩種方式示例
  • 如何用 Python 子進(jìn)程關(guān)閉 Excel 自動化中的彈窗
  • python清理子進(jìn)程機(jī)制剖析
  • python使用Queue在多個子進(jìn)程間交換數(shù)據(jù)的方法

標(biāo)簽:潮州 珠海 湖北 佳木斯 西寧 盤錦 上饒 宜昌

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python的子線程和子進(jìn)程是如何手動結(jié)束的?》,本文關(guān)鍵詞  Python,的,子,線程,和,進(jìn)程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python的子線程和子進(jìn)程是如何手動結(jié)束的?》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python的子線程和子進(jìn)程是如何手動結(jié)束的?的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 扎鲁特旗| 赤壁市| 班玛县| 托里县| 兴业县| 浙江省| 秦安县| 克东县| 海兴县| 丁青县| 平和县| 灵丘县| 平陆县| 周口市| 洛浦县| 广德县| 教育| 罗平县| 广州市| 佛学| 玛沁县| 定襄县| 阳原县| 朝阳市| 泗水县| 郑州市| 张家口市| 商水县| 新乡市| 共和县| 灵川县| 淅川县| 社会| 兰考县| 兴安盟| 宜都市| 临邑县| 宁乡县| 甘德县| 云浮市| 贵定县|