python繼承threading.Thread實(shí)現(xiàn)有返回值的子類實(shí)例
繼承與threading.Thread實(shí)現(xiàn)有返回值的子類MyThread,廢話不多說,大家直接看代碼
import threading
class MyThread(threading.Thread):
def __init__(self,func,args=()):
super(MyThread,self).__init__()
self.func = func
self.args = args
def run(self):
self.res = self.func(*self.args)
def getResult(self):
try:
return self.res
except Exception:
return None
補(bǔ)充知識(shí):python3多線程自定義threading子類
解決問題
1、python3多線程自定義threading.Thread的子類;
2、多線程并行,獲取多線程運(yùn)行結(jié)果
代碼實(shí)例
import threading
from time import sleep
exitFlag = True
def pp1(*args):
i = 1
while(exitFlag):
print('\r'+' '*20,end='')
print('\r線程1運(yùn)行中'+'.'*(i%7),end='')
sleep(0.5)
i = (i>=6 and 1 or i+1) #if i>=6則i=1,否則i=i+1
print('線程1結(jié)束')
def pp2(x,y):
sleep(3)
print('\n線程2結(jié)束')
return x + y
class MyThread(threading.Thread): #MyThread類繼承threading.Thread類
def __init__(self,func,args1=None,args2=None):
threading.Thread.__init__(self)
self.func = func
self.args1 = args1
self.args2 = args2
def run(self): #t.start()語句調(diào)用run方法
self.result = self.func(self.args1,self.args2)
def getResult(self): #getResult方法可獲得func函數(shù)return的結(jié)果
threading.Thread.join(self)
return self.result
t1 = MyThread(pp1) #初始化t1
t2 = MyThread(pp2,2,3) #初始化t1
t1.start() #啟動(dòng)線程t1
t2.start() #啟動(dòng)線程t2
t2.join() #判斷線程t2運(yùn)行結(jié)束
exitFlag = False #線程2運(yùn)行結(jié)束后,線程1才能結(jié)束
t1.join() #判斷線程t1運(yùn)行結(jié)束,線程t1結(jié)束后,主線程才能往下運(yùn)行
print('線程2返回結(jié)果: ',t2.getResult())
print('主線程結(jié)束')
以上這篇python繼承threading.Thread實(shí)現(xiàn)有返回值的子類實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python中threading庫實(shí)現(xiàn)線程鎖與釋放鎖
- Python多線程編程之threading模塊詳解
- Python 多線程之threading 模塊的使用
- python中threading和queue庫實(shí)現(xiàn)多線程編程
- Python threading模塊condition原理及運(yùn)行流程詳解
- Python多線程threading創(chuàng)建及使用方法解析
- Python3 socket即時(shí)通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程)
- Python中使用threading.Event協(xié)調(diào)線程的運(yùn)行詳解
- 淺談Python中threading join和setDaemon用法及區(qū)別說明
- python中threading開啟關(guān)閉線程操作
- python threading模塊的使用指南
相關(guān)文章
Python?sklearn?中的?make_blobs()?函數(shù)示例詳解
make_blobs()?是?sklearn.datasets中的一個(gè)函數(shù),這篇文章主要介紹了Python?sklearn?中的?make_blobs()?函數(shù),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Python Mysql數(shù)據(jù)庫操作 Perl操作Mysql數(shù)據(jù)庫
python對(duì)mysql數(shù)據(jù)庫的一些操作實(shí)現(xiàn)代碼2009-01-01
Python中的命名元組簡單而強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)示例詳解
namedtuple是Python中一個(gè)非常有用的數(shù)據(jù)結(jié)構(gòu),它提供了一種簡單的方式創(chuàng)建具有固定字段的輕量級(jí)對(duì)象,通過使用namedtuple,可以提高代碼的可讀性和可維護(hù)性,避免了使用類定義對(duì)象的復(fù)雜性,這篇文章主要介紹了Python中的命名元組簡單而強(qiáng)大的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2024-05-05
Python內(nèi)置函數(shù)的用法實(shí)例教程
這篇文章主要介紹了Python內(nèi)置函數(shù)的用法,包括求絕對(duì)值的abs()函數(shù)及數(shù)值類型轉(zhuǎn)換函數(shù)等,需要的朋友可以參考下2014-09-09
python3.6+django2.0+mysql搭建網(wǎng)站過程詳解
這篇文章主要介紹了python3.6+django2.0+mysql搭建網(wǎng)站過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python pandas 的索引方式 data.loc[],data[][]示例詳解
這篇文章主要介紹了Python pandas 的索引方式 data.loc[], data[][]的相關(guān)資料,其中data.loc[index,column]使用.loc[ ]第一個(gè)參數(shù)是行索引,第二個(gè)參數(shù)是列索引,本文結(jié)合實(shí)例代碼講解的非常詳細(xì),需要的朋友可以參考下2023-02-02

