selenium+python之python多線程

來源:互聯網
上載者:User

標籤:java   進位   使用   條件   處理   軌跡   super   一個   nbsp   

程式、進程及線程的區別

電腦程式是磁碟中可執行檔位元據(或者其他類型)他們只有在被讀取到記憶體中,被作業系統調用才開始他們的生命週期。

進程是程式的一次執行,每個進程都有自己的地址空間,記憶體,資料棧,以及其他記錄其運行軌跡的輔助資料,作業系統管理再其上面啟動並執行所有進程,並為這些進程公平得分配時間。

線程與進程相似,不同的是所有的線程都運行在同一個進程中,共用相同的運行環境。

 

1.單線程

單線程時,當處理器需要處理多個任務時,必須對這些任務安排執行的順序,並按照這個順序來執行任務。

 1 from time import sleep, ctime 2  3  4 # 聽音樂 5 def music(): 6     print(‘i was listening to music! %s‘ % ctime()) 7     sleep(2) 8  9 10 # 看電影11 def movie():12     print(‘i was at the movies! %s‘ % ctime())13     sleep(5)14 15 16 if __name__ == ‘__main__‘:17     music()18     movie()19     print(‘all end:‘, ctime())

增加迴圈功能:

 1 from time import sleep, ctime 2  3  4 # 聽音樂 5 def music(func, loop): 6     for i in range(loop): 7         print(‘i was listening to music! %s !%s‘ % (func, ctime())) 8         sleep(2) 9 10 11 # 看電影12 def movie(func, loop):13     for i in range(loop):14         print(‘i was listening to music! %s !%s‘ % (func, ctime()))15         sleep(5)16 17 18 if __name__ == ‘__main__‘:19     music(‘愛情買賣‘, 2)20     movie(‘一代宗師‘, 2)21     print(‘all end:‘, ctime())

給music()和movie()兩個函數設定參數:播放檔案和播放次數。函數中通過for迴圈控制播放的次數。

2、多線程

python通過兩個標準庫thread和threading提供對線程的支援。thread提供了低層級的,原始的線程以及一個簡單的鎖。threading基於Java的執行緒模式設計。鎖(lock)和條件變數(condition)在Java中時對象的基本行為(每個對象都內建了鎖和條件變數),而在python中則是獨立的對象。

(1)threading模組

避免使用thread模組,原因是它不支援守護線程。當主線程退出時,所有的子線程不關他們是否還在工作,都會被強行退出。但是我們並不希望發生這種行為。就要引入守護線程的概念。threading支援守護線程。

 1 from time import sleep, ctime 2 import threading 3  4  5 # 聽音樂 6 def music(func, loop): 7     for i in range(loop): 8         print(‘i was listening to music! %s !%s‘ % (func, ctime())) 9         sleep(2)10 11 12 # 看電影13 def movie(func, loop):14     for i in range(loop):15         print(‘i was listening to music! %s !%s‘ % (func, ctime()))16         sleep(5)17 18 19 # 建立線程數組20 threads = []21 # 建立線程t1,並添加到線程數組22 t1 = threading.Thread(target=music, args=(‘愛情買賣‘, 2))23 threads.append(t1)24 # 建立線程t2,並添加到線程數組25 t2 = threading.Thread(target=music, args=(‘一代宗師‘, 2))26 threads.append(t2)27 28 if __name__ == ‘__main__‘:29     # 啟動線程30     for t in threads:31         t.start()32     # 守護線程33     for t in threads:34         t.join()35 36     print(‘all end:‘, ctime())

註:import threading: 引入線程模組

       threads = []:建立線程數組,用於裝載線程。

       threading.Thread(): 通過調用threading模組的Thread()方法來建立線程。

運行結果如下:

從上面啟動並執行結果可以看出,兩個子線程(music,movie)同時啟動於10分15秒,知道所有線程結束於10分17秒共好使2秒。從執行的結果可以看出兩個線程達到了並行工作。

最佳化線程的建立

從上面例子中發現線程的建立很麻煩,每建立一個線程都需要一個t(t1,t2.。。。。)當建立的線程較多時,這樣的操作及其的不方便。

 1 from time import sleep, ctime 2 import threading 3  4  5 # 建立超級播放器 6 def super_player(file_, loop): 7     for i in range(2): 8         print(‘start playing: %s !%s‘ % (file_, ctime())) 9         sleep(2)10 # 播放檔案與播放時間長度11 lists = {‘愛情買賣.mp3‘:3,‘阿凡達.mp4‘:5,‘傳奇.mp3‘:4}12 13 threads = []14 files = range(len(lists))15 print(files)16 # 建立線程17 print(lists.items())18 for file_,time in lists.items():19     t = threading.Thread(target=super_player,args=(file_,time))20     print(t)21     threads.append(t)22 23 if __name__ == ‘__main__‘:24     # 啟動線程25     for t in files:26         threads[t].start()27     # 守護線程28     for t in files:29         threads[t].join()30 31     print(‘ end:%s‘% ctime())

 

 

selenium+python之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.