1. 程式人生 > >python 多執行緒 threading

python 多執行緒 threading

(python提供了兩個模組來實現多執行緒thread 和threading ,thread 有一些缺點,在threading 得到了彌補,為了不浪費你和時間,所以我們直接學習threading 就可以了。)

# 單執行緒 我想做聽音樂和看電影兩件事兒,那麼一定要先排一下順序。

from time import ctime, sleep

def music():
    for i in range(2):
        print("I was listening to music. %s" % ctime())
        sleep(2)
        
def move():
    for i in range(2):
        print("I was at the movies! %s" % ctime())
        sleep(5)
        
if __name__ == '__main__':
    music()
    move()
    print("all over %s" % ctime())

=========================== RESULT ================================

I was listening to music. Mon Sep  4 11:00:08 2017
I was listening to music. Mon Sep  4 11:00:10 2017
I was at the movies! Mon Sep  4 11:00:12 2017
I was at the movies! Mon Sep  4 11:00:17 2017
all over Mon Sep  4 11:00:22 2017

# 單執行緒 music()和move()更應該被看作是音樂和視訊播放器,至於要播放什麼歌曲和視訊應該由我們使用時決定

import threading
from time import ctime, sleep

def music(func):
    for i in range(2):
        print("I was listening to %s. %s" % (func, ctime()))
        sleep(2)

def move(func):
    for i in range(2):
        print("I was at the %s! %s" % (func, ctime()))
        sleep(5)

if __name__ == '__main__':
    music("一生所愛")
    move("敦刻爾克")
    print("all over %s" % ctime())

=========================== RESULT ================================

I was listening to 一生所愛. Mon Sep  4 11:12:39 2017
I was listening to 一生所愛. Mon Sep  4 11:12:40 2017
I was at the 敦刻爾克! Mon Sep  4 11:12:42 2017
I was at the 敦刻爾克! Mon Sep  4 11:12:47 2017
all over Mon Sep  4 11:12:52 2017

# 多執行緒 引入threadring來同時播放音樂和視訊

import threading
from time import ctime, sleep


def music(func):
    for i in range(2):
        print("I was listening to %s. %s" % (func, ctime()))
        sleep(2)


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=("一生所愛",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻爾克",))
threads.append(t2)


if __name__ == '__main__':
    for t in threads:
        t.setDaemon(True)
        t.start()

    print("all over %s" % ctime())

=========================== RESULT ================================

I was listening to 一生所愛. Mon Sep  4 11:21:01 2017
I was at the 敦刻爾克! Mon Sep  4 11:21:01 2017
all over Mon Sep  4 11:21:01 2017

註釋:從執行結果來看,子執行緒(muisc 、move )和主執行緒(print "all over %s" %ctime())都是同一時間啟動,但由於主執行緒執行完結束,所以導致子執行緒也終止。

# 多執行緒 繼續調整程式
# 對上面的程式加了個join()方法,用於等待執行緒終止。join()的作用是,在子執行緒完成執行之前,這個子執行緒的父執行緒將一直被阻塞
# join()方法的位置是在for迴圈外的,也就是說必須等待for迴圈裡的兩個程序都結束後,才去執行主程序


import threading
from time import ctime, sleep


def music(func):
    for i in range(2):
        print("I was listening to %s. %s" % (func, ctime()))
        sleep(2)


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=("一生所愛",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻爾克",))
threads.append(t2)


if __name__ == '__main__':
    for t in threads:
        t.setDaemon(True)
        t.start()

    t.join()

    print("all over %s" % ctime())

=========================== RESULT ================================

I was listening to 一生所愛. Mon Sep  4 11:24:56 2017
I was at the 敦刻爾克! Mon Sep  4 11:24:56 2017
I was listening to 一生所愛. Mon Sep  4 11:24:58 2017
I was at the 敦刻爾克! Mon Sep  4 11:25:01 2017
all over Mon Sep  4 11:25:06 2017

注:更詳細的使用請參考其它文件或資料

https://docs.python.org/3/library/threading.html

https://docs.python.org/2/library/threading.html