剛開始時候,cpu不支援多工,所以是順序執行的,比如先聽歌,再看電影
fromtime import ctime,sleep
def music():
for i in range(2
): print(
"i was listening to music. %s
",ctime())
sleep(1)
def move():
for i in range(2
): print(
"i was at the movies! %s
",str(ctime()))
sleep(5)
if __name__ == '
__main__':
music()
move()
print(
"all over %s
" %ctime())
後來,cpu支援了多工,那我們就可以一邊聽歌,一邊看電影了,兩個程式同時是執行狀態的。下面的例子是非阻塞的。
#coding=utf-8import threading
from
time import ctime,sleep
def music(func):
for i in range(2
): print(
"i was listening to .
".format(func,ctime()))
sleep(1)
def move(func):
for i in range(2
): print (
"i was at the !
".format(func,ctime()))
sleep(5)
threads =
t1 = threading.thread(target=music,args=(u'
愛情買賣
',))
t2 = threading.thread(target=move,args=(u'
阿凡達'
,))if __name__ == '
__main__':
for t in
threads:
t.setdaemon(false)
t.start()
print(
"all over %s
" %ctime())
輸出
i was listening to 愛情買賣. fri jan 1814:36:20
2019
i was at the 阿凡達! fri jan 18
14:36:20
2019
all over fri jan
1814:36:20
2019
i was listening to 愛情買賣. fri jan
1814:36:21
2019
i was at the 阿凡達! fri jan 18
14:36:25
2019
[program finished]
阻塞模式,由於主線程執行完畢,子執行緒不再繼續執行
#coding=utf-8import threading
from
time import ctime,sleep
def music(func):
for i in range(2
): print(
"i was listening to .
".format(func,ctime()))
sleep(1)
def move(func):
for i in range(2
): print (
"i was at the !
".format(func,ctime()))
sleep(5)
threads =
t1 = threading.thread(target=music,args=(u'
愛情買賣
',))
t2 = threading.thread(target=move,args=(u'
阿凡達'
,))if __name__ == '
__main__':
for t in
threads:
t.setdaemon(true)
t.start()
print(
"all over %s
" %ctime())
輸出
i was listening to 愛情買賣. fri jan 1814:38:36
2019
i was at the 阿凡達! fri jan 18
14:38:36
2019
all over fri jan
1814:38:36
2019
[program finished]
我們只對上面的程式加了個join()方法,用於等待執行緒終止。join()的作用是,在子執行緒完成執行之前,這個子執行緒的父執行緒將一直被阻塞。
注意: join()方法的位置是在for迴圈外的,也就是說必須等待for迴圈裡的兩個程序都結束後,才去執行主程序。
#coding=utf-8import threading
from
time import ctime,sleep
def music(func):
for i in range(2
): print(
"i was listening to .
".format(func,ctime()))
sleep(1)
def move(func):
for i in range(2
): print (
"i was at the !
".format(func,ctime()))
sleep(5)
threads =
t1 = threading.thread(target=music,args=(u'
愛情買賣
',))
t2 = threading.thread(target=move,args=(u'
阿凡達'
,))if __name__ == '
__main__':
for t in
threads:
t.setdaemon(true)
t.start()
t.join()
print(
"all over %s
" %ctime())
輸出
i was listening to 愛情買賣. fri jan 1814:47:26
2019
i was at the 阿凡達! fri jan 18
14:47:26
2019
i was listening to 愛情買賣. fri jan
1814:47:27
2019
i was at the 阿凡達! fri jan 18
14:47:31
2019
all over fri jan
1814:47:36
2019
[program finished]
python多執行緒 python多執行緒
通常來說,多程序適用於計算密集型任務,多執行緒適用於io密集型任務,如網路爬蟲。關於多執行緒和多程序的區別,請參考這個 下面將使用python標準庫的multiprocessing包來嘗試多執行緒的操作,在python中呼叫多執行緒要使用multiprocessing.dummy,如果是多程序則去掉...
python多執行緒詳解 Python多執行緒詳解
前言 由於最近的工作中一直需要用到python去處理資料,而在面對大量的資料時,python多執行緒的優勢就展現出來了。因而藉此機會,盡可能詳盡地來闡述python多執行緒。但對於其更底層的實現機制,在此不做深究,僅是對於之前的一知半解做個補充,也希望初學者能夠通過這篇文章,即便是照葫蘆畫瓢,也能夠...
python程式多執行緒 PYTHON多執行緒
在單執行緒的情況下,程式是逐條指令順序執行的。同一時間只做乙個任務,完成了乙個任務再進行下乙個任務。比如有5個人吃飯,單執行緒一次只允許乙個人吃,乙個人吃完了另乙個人才能接著吃,假如每個人吃飯都需要1分鐘,5個人就需要5分鐘。多執行緒的情況下,程式就會同時進行多個任務,雖然在同一時刻也只能執行某個任...