日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python中的線程和多線程是什么

一、線程的概念 

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

什么時候需要開啟多個線程:一個進程里面的多個線程共享這個進程里面的資源,因此如果多個任務共享同一塊資源的時候,需要開啟多個線程。 多線程指的是,在一個進程中開啟多個線程,簡單的說:如果多個任務共用同一個資源空間,那么必須在一個進程內開啟多個線程。一個進程這個任務里面可能對應多個分任務,如果一個進程里面只開啟一個線程的話,多個分任務之間實際上是串行的執(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)調用而阻塞,那么整個進程都會被掛起。

三、多線程的理解

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

相關推薦:《Python視頻教程》

四、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是阻塞當前線程,即使得在當前線程結束時,不會退出。從結果可以看到,主線程直到Thread-1結束之后才結束。

Python中,默認情況下,如果不加join語句,那么主線程不會等到當前線程結束才結束,但卻不會立即殺死該線程。如不加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語句,那么當主線程結束之后,會殺死子線程。

代碼:

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秒,就自動結束,并殺死子線程。如果join不加等待時間,t.join(),就會一直等待,一直到子線程結束,輸出如下:

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

相關推薦:

Python中的多進程是什么


本文題目:創(chuàng)新互聯(lián)Python教程:Python中的線程和多線程是什么
本文鏈接:http://www.5511xx.com/article/dhcpded.html