036線程進程(重要),036線程進程

來源:互聯網
上載者:User

036線程進程(重要),036線程進程

相比Java,python簡單很多,一個例子就可以理解
###########################
有兩種建立線程的方式,第一種是直接建立線程,把方法放進去,這是第一種:
建立線程對象,參數設定要執行的方法以及方法的參數,然後執行start()方法,如下:

 1 import  threading 2 import  time 3  4 def foo(n): 5     print('foo%s'%n) 6     time.sleep(1) 7     print('foo end') 8  9 def  bar(n):10     print('bar%s'%n)11     time.sleep(2)12     print('bar end')13 14 begin = time.time()15 16 t1 = threading.Thread(target=foo,args=(1,))    # 建立線程對象,target是某個方法的地址,args是實參17 t2 = threading.Thread(target=bar,args=(2,))18 19 t1.start()      # 開啟線程對象20 t2.start()21 print('main')22 23 end = time.time()24 print(end - begin)
線程1

運行結果:

GIL:cpython解譯器只能使用一個cpu,cpython裡面會自動加個大鎖,就是python不能夠使用多核,不能發揮多核的作用
改進方案就是使用多進程,/笑哭,或者運用到多計算和多I/O的,使用c語言
開子進程是複製父進程的資源,十分消耗資源,所以難搞

################
t.join()方法,可以阻塞住進程,一定要等t這個線程對象的線程運行完成才能繼續執行後面的代碼


##############################################
##守護線程Damen

t.setDaemon(True)設定成True後,主線程結束它就會結束

#####
threading.current_thread()        # 拿到這個執行線程的名字
threading.active_count()           # 活躍的線程

##############
第二種建立線程的方式:繼承線程類,覆蓋run方法,然後建立對象,執行start()方法,就可以執行這個線程了,該線程執行的代碼就是run裡面的代碼

import threadingimport timeclass MyThread(threading.Thread):    def __init__(self,num):        threading.Thread.__init__(self)            # 注意這個構造方法        self.num = num    def run(self):#定義每個線程要啟動並執行函數        print('runningonnumber:%s'%self.num)        time.sleep(3)if __name__ == '__main__':    t1 = MyThread(1)    t2 = MyThread(2)    t1.start()    t2.start()
線程2

 

 

兩個另外的例子:

# 01 要多線程執行的方法作為參數建立線程

import threadingimport timeclass Person_thread:    def __init__(self,name):        self.pname = name    def show(self):        for i in range(100):            print(self.pname,'*******',threading.current_thread(),'--------',i)            time.sleep(0.00001)if __name__ == '__main__':    p1 = Person_thread('xxx')    p2 = Person_thread('qqq')    t1 = threading.Thread(target=p1.show)    t2 = threading.Thread(target=p2.show)    t1.start()    t2.start()
View Code

# 02 繼承threading.Thread類

import threadingimport timeclass Person_thread(threading.Thread):    def __init__(self,name):        threading.Thread.__init__(self)        self.pname = name    def show(self):        for i in range(100):            print(self.pname,'*******',self.name,'--------',i)            time.sleep(0.00001)    def run(self):        self.show()if __name__ == '__main__':    p1 = Person_thread('xxx')    p2 = Person_thread('qqq')    p1.start()    p2.start()
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.