day9-Python學習筆記(二十二)多線程,多進程

來源:互聯網
上載者:User

標籤:pos   port   def   www   requests   reads   python3   span   設定   

多線程,多進程

 

多線程:
咱們開啟的程式都一個進程。
線程是包含在進程裡的。
進程裡面最少有一個線程
線程之間是互相獨立的
主線程

 

cpu是幾核的,就只能同時運行幾個進程

 

python的多線程是利用不了多核cpu的,GIL 全域解譯器鎖的
如果想利用多核cpu的話,就是用多進程

 

I0密集型任務
使用io比較多的 多線程
cpu密集型任務
多進程 能使用多核cpu
使用cpu比較多的

 

 

多線程.py

 

import threading
import time
import requests

def downHtml(url,name):
conent = requests.get(url).content
f = open(name+‘.html‘,‘wb‘)
f.write(conent)
f.close()
urls = [
[‘nnzhp‘,‘http://www.nnzhp.cn‘],
[‘dsx‘,‘http://www.imdsx.cn‘],
[‘besttest‘,‘http://www.besttest.cn‘]
]
start_time = time.time()
threads = [] #存放剛才啟動線程
for url in urls:
t = threading.Thread(target=downHtml,args=(url[1],url[0]))
t.start()
threads.append(t)

for t in threads: #等待3個子線程
t.join() #主線程等待子線程

end_time = time.time()
print(end_time-start_time)

# start_time = time.time()
# for url in urls:
# downHtml(url[1],url[0])
# end_time = time.time()
# print(end_time-start_time)
# for i in range(10):
# t = threading.Thread(target=sayHi,args=(‘小黑‘,)) #啟動線程
# t.start()#運行

多線程2.py
import threading
urls = list(range(100))
# 100個 啟動5個線程
def p(urls):
for u in urls:
print(u)

for i in range(5):
# urls[:20] 每次取的值
# urls[20:40]
# urls[40:60]
# urls[60:80]
# urls[80:]
t = threading.Thread(target=p,args=())
t.start()

守護線程.py
import threading
import time
def pz():
time.sleep(2)
print(‘我是秦始皇的陪葬‘)
threads = []
for i in range(10):
t = threading.Thread(target=pz)
t.setDaemon(True)#設定子線程為守護線程
# 守護線程就是,一旦主線程執行結束,那麼子線程立刻結束,不管子線程有沒有運行完
t.start()
threads.append(t)
for t in threads:
t.join() #如果主線程等待子線程的話,那麼設定的守護線程就不好使了
print(‘done‘)

# 進程快
# 還是線程快
 
#如果多線程執行的函數 想擷取到結果的話,那麼就得把它的結果寫到一個list裡面
res = []
import requests
def lida(x,y):
res.append(x+y)
import threading
for i in range(5):
t = threading.Thread(target=lida,args=(i,i))
t.start()
print(res)

線程鎖
import threading
from threading import Lock
num = 0
lock = Lock() # 申請一把鎖
def run():
global num
lock.acquire() # 加鎖
num += 1
lock.release() # 解鎖
lis = []
for i in range(5):
t = threading.Thread(target=run)
t.start()
lis.append(t)
for t in lis:
t.join()
print(‘over‘, num)

#加鎖是為了多線程的時候同時修改一個資料的時候 有可能會導致資料不正確

#python3裡面鎖 你不加也無所謂,它會自動的給你加上鎖

多進程.py
import multiprocessing,threading
import time
def run2():
print(‘這是多線程啟動的‘)
def run():
time.sleep(2)
for i in range(5):
t = threading.Thread(target=run2)
t.start()
if __name__ == ‘__main__‘:
for i in range(5):
p = multiprocessing.Process(target=run)
p.start()



 

day9-Python學習筆記(二十二)多線程,多進程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.