python?包之?threading?多線程
一、創(chuàng)建一個(gè)線程
- 通過實(shí)例化
threading.Thread類創(chuàng)建線程
import threading
def func(s):
print(s)
if __name__ == '__main__':
# 創(chuàng)建線程
thread = threading.Thread(target=func, args=('hello',))
# 啟動(dòng)線程
thread.start()
# 等待線程結(jié)束
thread.join()二、創(chuàng)建多個(gè)線程
import threading
def func(s):
print(s)
if __name__ == '__main__':
thread = [
threading.Thread(target=func, args=('1', ))
threading.Thread(target=func, args=('2', ))
]
[t.start() for t in thread]
[t.join() for t in thread]三、線程同步
- 使用鎖實(shí)現(xiàn)線程同步
threading.Lock是直接通過_thread模塊擴(kuò)展實(shí)現(xiàn)的- 鎖只有“鎖定”和“非鎖定”兩種狀態(tài)
- 同一個(gè)線程獲取鎖后,如果在釋放鎖之前再次獲取鎖會(huì)導(dǎo)致當(dāng)前線程阻塞,除非有另外的線程來釋放鎖,如果只有一個(gè)線程,并且發(fā)生了這種情況,會(huì)導(dǎo)致這個(gè)線程一直阻塞下去,即形成了死鎖。
import time
import threading
# 創(chuàng)建鎖
lock = threading.Lock()
# 全局變量
global_resource = [None] * 5
def change_resource(para, sleep):
# 請(qǐng)求鎖
lock.acquire()
# 這段代碼如果不加鎖,第一個(gè)線程運(yùn)行結(jié)束后global_resource中是亂的,輸出為:結(jié)果是: ['hello', 'hi', 'hi', 'hello', 'hello']
# 第二個(gè)線程運(yùn)行結(jié)束后,global_resource中還是亂的,輸出為:結(jié)果是: ['hello', 'hi', 'hi', 'hi', 'hi']
global global_resource
for i in range(len(global_resource)):
global_resource[i] = para
time.sleep(sleep)
print("結(jié)果是:", global_resource)
# 釋放鎖
lock.release()
if __name__ == '__main__':
thread = [
threading.Thread(target=change_resource, args=('hi', 2))
threading.Thread(target=change_resource, args=('hello', 1))
]
[t.start() for t in thread]
[t.join() for t in thread]
# 結(jié)果是: ['hi', 'hi', 'hi', 'hi', 'hi']
# 結(jié)果是: ['hello', 'hello', 'hello', 'hello', 'hello']四、遞歸鎖
- 上面線程同步使用的是普通鎖,也就是只有鎖的狀態(tài),并不知道是哪個(gè)線程加的鎖
- 這樣的話使用普通鎖時(shí),對(duì)于一些可能造成死鎖的情況,可以考慮使用遞歸鎖來解決
- 遞歸鎖和普通鎖的差別在于加入了“所屬線程”和“遞歸等級(jí)”的概念
- 釋放鎖必須有獲取鎖的線程來進(jìn)行釋放
import time
import threading
# 使用成一個(gè)遞歸鎖就可以解決當(dāng)前這種死鎖情況
rlock_hi = rlock_hello = threading.RLock()
def test_thread_hi():
# 初始時(shí)鎖內(nèi)部的遞歸等級(jí)為1
rlock_hi.acquire()
print('線程test_thread_hi獲得了鎖rlock_hi')
time.sleep(2)
# 如果再次獲取同樣一把鎖,則不會(huì)阻塞,只是內(nèi)部的遞歸等級(jí)加1
rlock_hello.acquire()
print('線程test_thread_hi獲得了鎖rlock_hello')
# 釋放一次鎖,內(nèi)部遞歸等級(jí)減1
rlock_hello.release()
# 這里再次減,當(dāng)遞歸等級(jí)為0時(shí),其他線程才可獲取到此鎖
rlock_hi.release()
def test_thread_hello():
rlock_hello.acquire()
print('線程test_thread_hello獲得了鎖rlock_hello')
time.sleep(2)
rlock_hi.acquire()
print('線程test_thread_hello獲得了鎖rlock_hi')
rlock_hi.release()
rlock_hello.release()
if __name__ == '__main__':
thread = [
threading.Thread(target=test_thread_hi)
threading.Thread(target=test_thread_hello)
]
[t.start() for t in thread]
[t.join() for t in thread]五、信號(hào)鎖
- 一個(gè)信號(hào)量管理一個(gè)內(nèi)部計(jì)數(shù)器
acquire()方法會(huì)減少計(jì)數(shù)器,release()方法則增加計(jì)數(shù)器- 計(jì)數(shù)器的值永遠(yuǎn)不會(huì)小于零
- 當(dāng)調(diào)用
acquire()時(shí),如果發(fā)現(xiàn)該計(jì)數(shù)器為零,則阻塞線程 - 直到調(diào)用
release()方法使計(jì)數(shù)器增加。
import time
import threading
# 創(chuàng)建信號(hào)量對(duì)象,初始化計(jì)數(shù)器值為3
semaphore3 = threading.Semaphore(3)
def thread_semaphore(index):
# 信號(hào)量計(jì)數(shù)器減1
semaphore3.acquire()
time.sleep(2)
print('thread_%s is running...' % index)
# 信號(hào)量計(jì)數(shù)器加1
semaphore3.release()
if __name__ == '__main__':
# 雖然會(huì)有9個(gè)線程運(yùn)行,但是通過信號(hào)量控制同時(shí)只能有3個(gè)線程運(yùn)行
# 第4個(gè)線程啟動(dòng)時(shí),調(diào)用acquire發(fā)現(xiàn)計(jì)數(shù)器為0了,所以就會(huì)阻塞等待計(jì)數(shù)器大于0的時(shí)候
for index in range(9):
threading.Thread(target=thread_semaphore, args=(index, )).start()到此這篇關(guān)于python 包之 threading 多線程的文章就介紹到這了,更多相關(guān)threading 多線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中的class_static的@classmethod的巧妙用法
python中的class_static的@classmethod的使用 classmethod的使用,主要針對(duì)的是類而不是對(duì)象,在定義類的時(shí)候往往會(huì)定義一些靜態(tài)的私有屬性,今天通過示例代碼看下classmethod的妙用2021-06-06
Python實(shí)現(xiàn)TCP探測(cè)目標(biāo)服務(wù)路由軌跡的原理與方法詳解
這篇文章主要介紹了Python實(shí)現(xiàn)TCP探測(cè)目標(biāo)服務(wù)路由軌跡的原理與方法,結(jié)合實(shí)例形式分析了Python TCP探測(cè)目標(biāo)服務(wù)路由軌跡的原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
Python時(shí)間處理模塊time和datetime詳解
本文詳細(xì)介紹了Python中常用的時(shí)間處理模塊time和datetime,time模塊提供多種時(shí)間獲取和轉(zhuǎn)換功能,datetime模塊則在time的基礎(chǔ)上增加了日期和時(shí)間的組合處理,如datetime.now()獲取當(dāng)前日期時(shí)間,兩個(gè)模塊在日常編程中非常有用,尤其是在需要時(shí)間日期計(jì)算和轉(zhuǎn)換的場(chǎng)景下2024-10-10
Linux下遠(yuǎn)程連接Jupyter+pyspark部署教程
這篇文章主要為大家詳細(xì)介紹了Linux下遠(yuǎn)程連接Jupyter+pyspark部署教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Python爬取商家聯(lián)系電話以及各種數(shù)據(jù)的方法
今天小編就為大家分享一篇Python爬取商家聯(lián)系電話以及各種數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)
這篇文章主要為大家介紹了科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python中私有屬性“_“下劃線和“__“雙下劃線區(qū)別
本文主要介紹了Python中私有屬性“_“下劃線和“__“雙下劃線區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

