python中多執行緒程式設計 Python的多執行緒程式設計

2021-10-11 16:51:17 字數 4091 閱讀 1553

提到多執行緒,很多人就會望而卻步,本文將由淺入深地帶你攻克python多執行緒程式設計,並防止你跳入深坑,

首先看一段簡單的**:

1 from time importctime,sleep2 defplay_video(video):3 for i in range(2):4 print "i am playing video: %s at %s"%(video,ctime())5 sleep(4)6

8 defplay_music(music):9 for i in range(2):10 print "i am playing music: %s at %s"%(music,ctime())11 sleep(2)12

14 if __name__=="__main__":15

16 play_video("speed_and_crazy")17 play_music("chengdu")18

19 print "all are over at %s"%ctime()20

執行結果:

c:\python27>python mui_thread.py

i am playing video: speed_and_crazy at mon jun26 23:01:59 2017i am playing video: speed_and_crazy at mon jun26 23:02:03 2017i am playing music: chengdu at mon jun26 23:02:07 2017i am playing music: chengdu at mon jun26 23:02:09 2017all are over at mon jun26 23:02:11 2017

隨著人們對多工的要求,同時為了充分利用cpu資源,多執行緒程式設計不可避免,那麼我們如何利用python去實現play_video和play_music

兩個任務同時執行呢?

1 from time importctime,sleep2 importthreading3 defplay_video(video):4 for i in range(2):5 print "i am playing video: %s at %s \n"%(video,ctime())6 sleep(5)7

9 defplay_music(music):10 for i in range(2):11 print "i am playing music: %s at %s \n"%(music,ctime())12 sleep(1)13

14 threads=15

16 thread1=threading.thread(target=play_video,args=("speed_and_crazy",))17

20 thread2=threading.thread(target=play_music,args=("chengdu",))21

26 if __name__=="__main__":27

28 for thread inthreads:29 thread.setdaemon(true) #將執行緒宣告為守護執行緒,必須在start()方法呼叫之前,如果不設定為守護執行緒,程式會被無限掛起30 thread.start()31

32 print "all are over at %s \n"%ctime()33

測試結果:

c:\python27>python mui_thread.py

i am playing video: speed_and_crazy at mon jun26 23:18:52 2017all are over at mon jun26 23:18:52 2017i am playing music: chengdu at mon jun26 23:18:52 2017 #從列印的時間可知,play_video、play_music和父程序幾乎同時執行

從結果看,與我們最初的目標相差甚遠,怎麼沒有按照順序執行,為什麼每個函式都只有一條日記輸出?

那是因為子執行緒(play_video、play_music)和主線程print "all are over at %s \n"%ctime()都是同一時間啟動,但由於主線程已經執行結束,所以導致子執行緒也同時終止,在這種條件下,我們如何保證子程序都能夠執行完畢呢?

增加thread.join()並 放在迴圈外

from time importctime,sleepimportthreadingdefplay_video(video):for i in range(2):print "i am playing video: %s at %s \n"%(video,ctime())

sleep(1)defplay_music(music):for i in range(2):print "i am playing music: %s at %s \n"%(music,ctime())

sleep(5)

threads=

thread1=threading.thread(target=play_video,args=("speed_and_crazy",))

thread2=threading.thread(target=play_music,args=("chengdu",))

thread.setdaemon(true)

thread.start()

thread.join() #加在迴圈外,print "all are over at %s \n"%ctime()

執行結果:

1 c:\python27>python mui_thread.py2 i am playing video: speed_and_crazy at mon jun 26 23:32:21 2017

3 i am playing music: chengdu at mon jun 26 23:32:21 2017

6 i am playing video: speed_and_crazy at mon jun 26 23:32:22 2017

8 i am playing music: chengdu at mon jun 26 23:32:26 2017

10 all are over at mon jun 26 23:32:31 2017

thread.join()的作用是主線程必須等待子執行緒都執行完了才能結束,play_video、play_music幾乎同時執行

但是如果改變play_video、play_music裡面的sleep的時長,即是下面的**:

from time importctime,sleepimportthreadingdefplay_video(video):for i in range(2):print "i am playing video: %s at %s \n"%(video,ctime())

sleep(5)defplay_music(music):for i in range(2):print "i am playing music: %s at %s \n"%(music,ctime())

sleep(1)

threads=

thread1=threading.thread(target=play_video,args=("speed_and_crazy",))

thread2=threading.thread(target=play_music,args=("chengdu",))

thread.setdaemon(true)

thread.start()

thread.join()print "all are over at %s \n"%ctime()

此時執行結果:

c:\python27>python mui_thread.py

i am playing video: speed_and_crazy at mon jun26 23:44:13 2017i am playing music: chengdu at mon jun26 23:44:13 2017i am playing music: chengdu at mon jun26 23:44:14 2017all are over at mon jun26 23:44:15 2017

我們看到play_video還有一條log沒有列印出來,原因是thread.join()在迴圈外,此時的thread為play_music,父程序只會等待play_music程序執行完就結束,而不會等待play_video(sleep時間較長)執行完才結束,所以才會有上面的結果

python 多執行緒程式設計

一 執行緒基礎 1 建立執行緒 thread模組提供了start new thread函式,用以建立執行緒。start new thread函式成功建立後還可以對其進行操作。其函式原型 start new thread function,atgs kwargs 其引數含義如下 args 元組形式的引...

python 多執行緒程式設計

一 執行緒基礎 1 建立執行緒 thread模組提供了start new thread函式,用以建立執行緒。start new thread函式成功建立後還能夠對其進行操作。其函式原型 start new thread function,atgs kwargs 其引數含義例如以下 args 元組形式...

Python多執行緒程式設計

import threading import time deffunc name time.sleep 3 print 子執行緒 s 啟動 threading.current thread name print hello name print 子執行緒 s 結束 threading.curren...