什么是Python中的線程和多線程

什么是Python中的線程和多線程?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

創(chuàng)新互聯(lián)是專業(yè)的高碑店網(wǎng)站建設公司,高碑店接單;提供成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行高碑店網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

一、線程的概念 

一個進程里面至少有一個控制線程,進程的概念只是一種抽象的概念,真正在CPU上面調(diào)度的是進程里面的線程,就好比真正在地鐵這個進程里面工作的實際上是地鐵里面的線程,北京地鐵里面至少要有一個線程,線程是真正干活的,線程用的是進程里面包含的一堆資源,線程僅僅是一個調(diào)度單位,不包含資源。

什么時候需要開啟多個線程:一個進程里面的多個線程共享這個進程里面的資源,因此如果多個任務共享同一塊資源的時候,需要開啟多個線程。 多線程指的是,在一個進程中開啟多個線程,簡單的說:如果多個任務共用同一個資源空間,那么必須在一個進程內(nèi)開啟多個線程。一個進程這個任務里面可能對應多個分任務,如果一個進程里面只開啟一個線程的話,多個分任務之間實際上是串行的執(zhí)行效果,即一個程序里面只含有一條執(zhí)行路徑。

對于計算密集型應用,應該使用多進程;對于IO密集型應用,應該使用多線程。線程的創(chuàng)建比進程的創(chuàng)建開銷小的多。

二、Python中線程的特點 

1.在其他語言當中,一個進程里面開啟多個線程,每個線程都可以給一個cpu去使用,但是在 python當中,在同一時刻,一個進程當中只能有一個線程處于運行狀態(tài)。

2.比如在其他語言當中,比如我現(xiàn)在開啟了一個進程,這個進程當中含有幾個線程,如果我現(xiàn)在有多個cpu,每一個線程是可以對應相應的CPU的。 

3.但是在python當中,如果我們現(xiàn)在開啟了一個進程,這個進程里面對應多個線程,同一時刻只有一個線程可以處于運行狀態(tài)。 對于其他語言而言,在多CPU系統(tǒng)中,為了最大限度的利用多核,可以開啟多個線程。 但是Python中的多線程是利用不了多核優(yōu)勢的。    

4.在同一個進程當中,多個線程彼此之間可以相互通信;但是進程與進程之間的通信必須基于IPC這種 消息的通信機制(IPC機制包括隊列和管道)。在一個進程當中,改變主線程可能會影響其它線程的行為,但是改變父進程并不會影響其它子進程的行為,因為進程與進程之間是完全隔離的。 在python當中,在同一時刻同一進程當中只能同時有一個線程在運行,如果有一個線程使用了系統(tǒng)調(diào)用而阻塞,那么整個進程都會被掛起。

三、多線程的理解

多進程和多線程都可以執(zhí)行多個任務,線程是進程的一部分。線程的特點是線程之間可以共享內(nèi)存和變量,資源消耗少(不過在Unix環(huán)境中,多進程和多線程資源調(diào)度消耗差距不明顯,Unix調(diào)度較快),缺點是線程之間的同步和加鎖比較麻煩。

四、Python多線程創(chuàng)建

在Python中,同樣可以實現(xiàn)多線程,有兩個標準模塊thread和threading,不過我們主要使用更高級的threading模塊。使用例子:

import threading
import time
 
def target():
    print 'the curent threading  %s is running' % threading.current_thread().name
    time.sleep(1)
    print 'the curent threading  %s is ended' % threading.current_thread().name
 
print 'the curent threading  %s is running' % threading.current_thread().name
t = threading.Thread(target=target)
 
t.start()
t.join()
print 'the curent threading  %s is ended' % threading.current_thread().name

輸出:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  Thread-1 is ended
the curent threading  MainThread is ended

start是啟動線程,join是阻塞當前線程,即使得在當前線程結(jié)束時,不會退出。從結(jié)果可以看到,主線程直到Thread-1結(jié)束之后才結(jié)束。

Python中,默認情況下,如果不加join語句,那么主線程不會等到當前線程結(jié)束才結(jié)束,但卻不會立即殺死該線程。如不加join輸出如下:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  MainThread is ended
the curent threading  Thread-1 is ended

但如果為線程實例添加t.setDaemon(True)之后,如果不加join語句,那么當主線程結(jié)束之后,會殺死子線程。

代碼:

import threading
import time
def target():
    print 'the curent threading  %s is running' % threading.current_thread().name
    time.sleep(4)
    print 'the curent threading  %s is ended' % threading.current_thread().name
print 'the curent threading  %s is running' % threading.current_thread().name
t = threading.Thread(target=target)
t.setDaemon(True)
t.start()
t.join()
print 'the curent threading  %s is ended' % threading.current_thread().name

輸出如下:

the curent threading  MainThread is running
the curent threading  Thread-1 is runningthe curent threading  MainThread is ended

如果加上join,并設置等待時間,就會等待線程一段時間再退出:

import threading
import time
def target():
    print 'the curent threading  %s is running' % threading.current_thread().name
    time.sleep(4)
    print 'the curent threading  %s is ended' % threading.current_thread().name
print 'the curent threading  %s is running' % threading.current_thread().name
t = threading.Thread(target=target)
t.setDaemon(True)
t.start()
t.join(1)

輸出:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  MainThread is ended

主線程等待1秒,就自動結(jié)束,并殺死子線程。如果join不加等待時間,t.join(),就會一直等待,一直到子線程結(jié)束,輸出如下:

the curent threading  MainThread is running
the curent threading  Thread-1 is running
the curent threading  Thread-1 is ended
the curent threading  MainThread is ended

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

本文題目:什么是Python中的線程和多線程
鏈接分享:http://www.muchs.cn/article6/gdihig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、網(wǎng)站設計軟件開發(fā)、移動網(wǎng)站建設、網(wǎng)站營銷ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營