Python的子線程和子進(jìn)程是如何手動(dòng)結(jié)束的?
如何結(jié)束Python的子線程和子進(jìn)程
結(jié)束子線程的方法:
這個(gè)是搬運(yùn)其他大神的代碼,鄙人也不知道原理,反正拿來(lái)主義,暫時(shí)沒(méi)發(fā)現(xiàn)什么缺點(diǎn),先用著再說(shuō)。
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()
這里說(shuō)一下如果用類的話要如何寫,想結(jié)束子進(jìn)程或者子線程就需要拿到進(jìn)程對(duì)象或者線程對(duì)象,但是類中沒(méi)辦法實(shí)現(xiàn)創(chuàng)建一個(gè)類屬性的方式然后用self.×××的方式來(lái)在其他類方法中調(diào)用,這時(shí)候就創(chuàng)建一個(gè)類屬性list,然后創(chuàng)建好子進(jìn)程或者子線程后把對(duì)象賦值給這個(gè)list,再在類的其他方法中調(diào)用這個(gè)list中的元素,就拿到了子進(jìn)程或者子線程的對(duì)象了。
例如:
def startss(self,a1,b1,c1,under,rough,blue,among):
# 創(chuàng)建新線程
p1=threading.Thread(target=self.line01,args=(a1,b1,under,rough,)) #必須加,號(hào)
p2=threading.Thread(target=self.line02,args=(a1,c1,under,blue,))
p3=Process(target=self.Process01,args=(under,rough,blue,)) #計(jì)算進(jìn)程
#among是類屬性list容器
among.append(p1)
among.append(p2)
among.append(p3)
# 開啟新線程
p1.start()
p2.start()
#開啟計(jì)算用進(jìn)程
p3.start()
參考資料:http://www.dhdzp.com/article/185867.htm
python 多進(jìn)程如何終止或重啟子進(jìn)程
到此這篇關(guān)于Python的子線程和子進(jìn)程是如何手動(dòng)結(jié)束的?的文章就介紹到這了,更多相關(guān)Python的子線程和子進(jìn)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Python的高級(jí)數(shù)據(jù)結(jié)構(gòu)與算法
這篇文章主要介紹了關(guān)于Python的高級(jí)數(shù)據(jù)結(jié)構(gòu)與算法,掌握這些數(shù)據(jù)結(jié)構(gòu)和算法將幫助我們?cè)趯?shí)際編程中解決各種問(wèn)題,提高我們的編程技巧和水平,需要的朋友可以參考下2023-04-04
python產(chǎn)生模擬數(shù)據(jù)faker庫(kù)的使用詳解
這篇文章主要介紹了python產(chǎn)生模擬數(shù)據(jù)faker庫(kù)的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python實(shí)現(xiàn)PS圖像調(diào)整之對(duì)比度調(diào)整功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS圖像調(diào)整之對(duì)比度調(diào)整功能,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)PS圖像對(duì)比度調(diào)整的原理、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Python OpenCV中的numpy與圖像類型轉(zhuǎn)換操作
這篇文章主要介紹了Python OpenCV中的numpy與圖像類型轉(zhuǎn)換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12

