python 多執行緒多程序

2022-08-31 14:45:19 字數 3601 閱讀 8929

多執行緒,適用於io密集型任務

多程序,適用於cpu密集型任務

資料分析,演算法,依賴cpu來運算

程序就是多個資源的集合,

執行緒是包含在程序裡面的,執行緒和執行緒直接是相對獨立的

執行緒的優點

1.易於排程。

2.提高併發性。通過執行緒可方便有效地實現併發性。程序可建立多個執行緒來執行同一程式的不同部分。

3.開銷少。建立執行緒比建立程序要快,所需開銷很少。

4.利於充分發揮多處理器的功能。通過建立多執行緒程序,每個執行緒在乙個處理器上執行,從而實現應用程式的併發性,使每個處理器都得到充分執行。

def

down_load():

time.sleep(5)

print('

執行完了')

#threading.thread(target=down_load,args=('name','id')#target是函式名,args是函式的引數沒有可以不寫

t1 = threading.thread(target=down_load)

t1.start()

#啟動執行緒

每個子執行緒都是在主線程裡啟動的子執行緒

繼承呼叫

class test_thread(threading.thread):#

繼承threading的thread類

def__init__

(self,num):

threading.thread.

__init__(self)#

執行父類的構造方法

self.num =num

def run(self):#

執行多執行緒的函式

while self.num>0:

print(threading.active_count())#

列印當前執行緒數量

self.num-=1

for i in range(5):

t = test_thread(2)#

生成例項

t.start()#

啟動執行緒

python不是真正意義的多執行緒,python**的執行由python 虛擬機器(也叫直譯器主迴圈,cpython版本)來控制,python 在設計之初就考慮到要在直譯器的主迴圈中,同時只有乙個執行緒在執行,即在任意時刻,只有乙個執行緒在直譯器中執行。對python 虛擬機器的訪問由全域性直譯器鎖(gil)來控制,正是這個鎖能保證同一時刻只有乙個執行緒在執行。cpu有幾個核心就能夠同時執行幾個任務,python無法利用多核就是因為它有全域性直譯器鎖(gil)

執行緒池,執行緒池在系統啟動時即建立大量空閒的執行緒,程式只要將乙個函式提交給執行緒池,執行緒池就會啟動乙個空閒的執行緒來執行它。當該函式執行結束後,該執行緒並不會死亡,而是再次返回到執行緒池中變成空閒狀態,等待執行下乙個函式。此外,使用執行緒池可以有效地控制系統中併發執行緒的數量。當系統中包含有大量的併發執行緒時,會導致系統效能急劇下降,甚至導致 python直譯器崩潰,而執行緒池的最大執行緒數引數可以控制系統中併發執行緒的數量不超過此數

import

threadpool,requests

from hashlib import

md5url_list = ['

','',

'','']

def down_load(url):#

r =requests.get(url)

m =md5(url.encode())

with open(m.hexdigest()+'

.png

','wb

')as fw:

fw.write(r.content)

pool = threadpool.threadpool(10)#

例項化乙個執行緒池 threadpool例項化乙個物件pool,呼叫了構造函式引數是10

reqs = threadpool.makerequests(down_load,url_list)#

分配資料

for req in

reqs:

pool.putrequest(req)

#將執行緒放入執行緒池

#[pool.putrequest(req) for req in reqs]#列表生成試

pool.wait()#

等待執行緒池結束

print('

end')#

執行主線程

守護執行緒,主線程結束,守護執行緒馬上死掉

import

threading

import

time

class

fun():

defsum(self,a,b):

time.sleep(2)

print(a+b)

defcut(self,c,d):

time.sleep(5)

print(c-d)

t =fun()

t1 = threading.thread(target=t.sum,args=(1,2))

t2 = threading.thread(target=t.cut,args=(5,4))

t1.setdaemon(true)

#設定執行緒為守護執行緒

t2.setdaemon(true)#

設定執行緒為守護執行緒

t1.start()

t2.start()

print('

主線程執行完畢

')結果只會列印出'主線程執行完畢'

執行緒鎖,多個執行緒操作同乙個資料的時候 加鎖

num =0

lock = threading.lock()#

申請一把鎖

defadd():

global

num with lock:

#加鎖,解鎖

num+=1

for i in range(20):

t = threading.thread(target=add)

t.start()

print(num)

多程序

import multiprocessing,time

def down_load():

time.sleep(5)

print('執行完了')

if __name__ == '__main__':#windows系統要加,不然會報錯

for i in range(5):

p = multiprocessing.process(target=down_load)#分配5個程序執行函式

p.start()#啟動程序

p.join()#等待子程序。子執行緒互相等待

while len(multiprocessing.active_children())!=0:#等待子程序執行完在執行主程序

pass

print(multiprocessing.active_children())#檢視有多少個子程序

print(multiprocessing.current_process())#主程序

python多執行緒 多程序

background task once join is used whether deamon attribute is true is not importantonly useful when the main program is running ok to kill starmap和map...

python多執行緒 多程序

threading相對與thread是更高階別的執行緒管理模組 thread和threading模組中的一些屬性會有衝突 thread模組擁有的同步原因實際上只有乙個lock,而threading有很多 lock,semaphore等 使用thread模組執行緒,當主線程結束時其子執行緒也會被強制結...

python多執行緒多程序

執行緒等待,多執行緒在執行的時候,每個執行緒都是獨立執行的,不受其他的執行緒干擾,如果想在哪個執行緒執行完之後,再做其他操作的話,就得等待它完成,那怎麼等待呢,使用join,等待執行緒結束 for t in threads 等待3個子執行緒 t.join 主線程等待子執行緒 end time tim...