- 1、如何理解python的多線程編程
- 2、Python多線程
- 3、python 多線程
- 4、Python多線程的一些問題
線程是程序員必須掌握的知識,多線程對於代碼的並發執行、提升代碼效率和運行都至關重要。今天就分享一個黑馬程序員Python多線程編程的教程,從0開始學習python多任務編程,想了解python高並發實現,從基礎到實踐,通過知識點 + 案例教學法幫助你想你想迅速掌握python多任務。
課程內容:
1.掌握多任務實現的並行和並發
2.掌握多進程實現多任務
3.掌握多線程實現多任務
4.掌握合理搭配多進程和線程
適用人群:
1、對python多任務編程感興趣的在校生及應屆畢業生。
2、對目前職業有進一步提升要求,希望從事python人工智能行業高薪工作的在職人員。
3、對python人工智能行業感興趣的相關人員。
基礎課程主講內容包括:
1.python多任務編程
基礎班課程大綱:
00-課程介紹
01-多任務介紹
02-進程介紹
03-使用多進程來完成多任務
04-多進程執行帶有參數的任務
05-獲取進程的編號
06-進程注意點
07-案例-多進程實現傳智視頻文件夾多任務拷貝器
08-線程介紹
09-使用多線程執行多任務
10-線程執行帶有參數的任務
11-主線程和子線程的結束順序
12-線程之間的執行順序是無序
13-線程和進程的對比
14-案例-多線程實現傳智視頻文件夾多任務拷貝器
15-課程總結
那是當然。你這樣寫就可以了
self.p[:]=array
這樣寫法的含義就是指針不變。只換內容。這樣就可以同步了。
你的寫法是,新建一個數組,再把指針緞帶self.p,如果其它的線程就會出問題。
另外你的p應該放在__init__之前。引用時使用T.p來引用,這樣更合理一些。
python支持多線程效果還不錯,很多方面都用到了python 多線程的知識,我前段時間用python 多線程寫了個處理生產者和消費者的問題,把代碼貼出來給你看下:
#encoding=utf-8
import threading
import random
import time
from Queue import Queue
class Producer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),’adding’,i,’to queue’
self.sharedata.put(i)
time.sleep(random.randrange(10)/10.0)
print self.getName(),’Finished’
# Consumer thread
class Consumer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),’got a value:’,self.sharedata.get()
time.sleep(random.randrange(10)/10.0)
print self.getName(),’Finished’
# Main thread
def main():
queue = Queue()
producer = Producer(‘Producer’, queue)
consumer = Consumer(‘Consumer’, queue)
print ‘Starting threads …’
producer.start()
consumer.start()
producer.join()
consumer.join()
print ‘All threads have terminated.’
if __name__ == ‘__main__’:
main()
如果你想要了解更多的python 多線程知識可以點下面的參考資料的地址,希望對有幫助!
python提供了兩個模塊來實現多線程thread 和threading ,thread 有一些缺點,在threading 得到了彌補,為了不浪費你和時間,所以我們直接學習threading 就可以了。
繼續對上面的例子進行改造,引入threadring來同時播放音樂和視頻:
#coding=utf-8import threadingfrom time import ctime,sleepdef music(func): for i in range(2): print “I was listening to %s. %s” %(func,ctime())
sleep(1)def move(func): for i in range(2): print “I was at the %s! %s” %(func,ctime())
sleep(5)
threads = []
t1 = threading.Thread(target=music,args=(u’愛情買賣’,))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u’阿凡達’,))
threads.append(t2)if __name__ == ‘__main__’: for t in threads:
t.setDaemon(True)
t.start() print “all over %s” %ctime()
import threading
首先導入threading 模塊,這是使用多線程的前提。
threads = []
t1 = threading.Thread(target=music,args=(u’愛情買賣’,))
threads.append(t1)
創建了threads數組,創建線程t1,使用threading.Thread()方法,在這個方法中調用music方法target=music,args方法對music進行傳參。 把創建好的線程t1裝到threads數組中。
接着以同樣的方式創建線程t2,並把t2也裝到threads數組。
for t in threads:
t.setDaemon(True)
t.start()
最後通過for循環遍曆數組。(數組被裝載了t1和t2兩個線程)
setDaemon()
setDaemon(True)將線程聲明為守護線程,必須在start() 方法調用之前設置,如果不設置為守護線程程序會被無限掛起。子線程啟動後,父線程也繼續執行下去,當父線程執行完最後一條語句print “all over %s” %ctime()後,沒有等待子線程,直接就退出了,同時子線程也一同結束。
start()
開始線程活動。
運行結果:
========================= RESTART ================================
I was listening to 愛情買賣. Thu Apr 17 12:51:45 2014 I was at the 阿凡達! Thu Apr 17 12:51:45 2014 all over Thu Apr 17 12:51:45 2014
從執行結果來看,子線程(muisc 、move )和主線程(print “all over %s” %ctime())都是同一時間啟動,但由於主線程執行完結束,所以導致子線程也終止。
繼續調整程序:
…if __name__ == ‘__main__’: for t in threads:
t.setDaemon(True)
t.start()
t.join() print “all over %s” %ctime()
我們只對上面的程序加了個join()方法,用於等待線程終止。join()的作用是,在子線程完成運行之前,這個子線程的父線程將一直被阻塞。
注意: join()方法的位置是在for循環外的,也就是說必須等待for循環里的兩個進程都結束後,才去執行主進程。
運行結果:
========================= RESTART ================================
I was listening to 愛情買賣. Thu Apr 17 13:04:11 2014 I was at the 阿凡達! Thu Apr 17 13:04:11 2014I was listening to 愛情買賣. Thu Apr 17 13:04:12 2014I was at the 阿凡達! Thu Apr 17 13:04:16 2014all over Thu Apr 17 13:04:21 2014
從執行結果可看到,music 和move 是同時啟動的。
開始時間4分11秒,直到調用主進程為4分22秒,總耗時為10秒。從單線程時減少了2秒,我們可以把music的sleep()的時間調整為4秒。
…def music(func): for i in range(2): print “I was listening to %s. %s” %(func,ctime())
sleep(4)
…
子線程啟動11分27秒,主線程運行11分37秒。
雖然music每首歌曲從1秒延長到了4 ,但通多程線的方式運行腳本,總的時間沒變化。
原創文章,作者:OZ7FU,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/126959.html