Python 多線程其他屬性以及繼承Thread類詳解
一、線程常用屬性
1.threading.currentThread:返回當前線程變量
2.threading.enumerate:返回一個包含正在運行的線程的list,正在運行的線程指的是線程啟動后,結束前的狀態(tài)
3.threading.activeCount:返回正在運行的線程數(shù)量,效果跟len(threading.enumer)一樣
4.thr.setName:給線程設置名字
5.thr.getName:得到線程的名字。
舉例:
mport _thread as thread import time def loop1(in1): print("Start loop 1 at:", time.ctime()) print("我是參數(shù)", in1) time.sleep(4) print("End loop 1 at:", time.ctime()) def loop2(in1, in2): print("Start loop 2 at:", time.ctime()) print("我是參數(shù)", in1, "和參數(shù) ", in2) time.sleep(4) print("End loop 2 at:", time.ctime()) import threading def main1(): print("Starting at:", time.ctime()) t1 = threading.Thread(target = loop1, args = ('', )) t1.setName("THR_1")# 給線程重命名 t1.start() t2 = threading.Thread(target = loop2, args = ('', '')) t2.setName("THR_2") t2.setDaemon(True)# 主線程運行完了就完了, 不用等線程2 t2.start() time.sleep(3)# 三秒后兩個子線程仍然在運行著, 因為他們里面有一個四秒在停著 for thr in threading.enumerate(): #返回的是正在運行的子線程的列表 print("正在運行的子線程名為:{0}".format(thr.getName()))# 讀取了該線程的名字 print("正在運行的子線程數(shù)量為:{0}".format(threading.activeCount()))# 打印出了線程的數(shù)量, 包括主線程和兩個子線程一共3個線程 t1.join()# 等線程1運行完了再接著向下運行 print("ALL done at :", time.ctime()) if __name__ == "__main__": main1()
二、直接繼承子類threading.Thread
1.直接繼承Thread;重寫run函數(shù)
2.例子:
class MyThread(threading.Thread): #定義一個Thread的子類 def __init__(self, args): #重寫__init__函數(shù), 其中參數(shù)為self和新引入的參數(shù) super(MyThread, self).__init__()# 固定格式, 繼承父類的__init__函數(shù) self.args = args def run(self): time.sleep(1) print("The args for this class is {0}".format(self.args)) for i in range(5): t = MyThread(i) t.start() t.join()
三、源碼
d24_3_other_multi_thread_attribute.py
https://github.com/ruigege66/Python_learning/blob/master/d24_3_other_multi_thread_attribute.py
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Python多線程編程(三):threading.Thread類的重要函數(shù)和方法
- Python中線程threading.Thread的使用詳解
- python中的多線程鎖lock=threading.Lock()使用方式
- Python?常用模塊threading和Thread模塊之線程池
- 詳解Python的多線程定時器threading.Timer
- Python中threading庫實現(xiàn)線程鎖與釋放鎖
- Python多線程編程之threading模塊詳解
- python GUI庫圖形界面開發(fā)之PyQt5線程類QThread詳細使用方法
- 對python:threading.Thread類的使用方法詳解
- Python線程threading(Thread類)
相關文章
Python3里的super()和__class__使用介紹
這篇文章主要介紹了Python3里的super()和__class__使用介紹,本文用實例講解了這兩個方法之間的關系,需要的朋友可以參考下2015-04-04python中列表添加元素的幾種方式(+、append()、extend())
本文主要介紹了python中列表添加元素的幾種方式(+、append()、extend()),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08matplotlib之pyplot模塊添加文本、注解(text和annotate)
matplotlib是python最著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合交互式地行制圖,下面這篇文章主要給大家介紹了關于matplotlib之pyplot模塊添加文本、注解(text和annotate)的相關資料,需要的朋友可以參考下2022-05-05Python實現(xiàn)棧的方法詳解【基于數(shù)組和單鏈表兩種方法】
這篇文章主要介紹了Python實現(xiàn)棧的方法,結合實例形式詳細分析了Python基于數(shù)組和單鏈表兩種方法定義棧的相關操作技巧,需要的朋友可以參考下2020-02-02python中ThreadPoolExecutor線程池和ProcessPoolExecutor進程池
這篇文章主要介紹了python中ThreadPoolExecutor線程池和ProcessPoolExecutor進程池,文章圍繞主題相關資料展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06Pandas.DataFrame時間序列數(shù)據(jù)處理的實現(xiàn)
本文主要介紹了Pandas.DataFrame時間序列數(shù)據(jù)處理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02