最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python?包之?threading?多線程

 更新時(shí)間:2022年04月02日 10:56:56   作者:autofelix  
這篇文章主要介紹了python?包之?threading?多線程,文章通過(guò)實(shí)例化threading.Thread類創(chuàng)建線程,下文相關(guān)資料介紹,需要的朋友可以參考一下

一、創(chuàng)建一個(gè)線程

  • 通過(guò)實(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是直接通過(guò)_thread模塊擴(kuò)展實(shí)現(xiàn)的
  • 鎖只有“鎖定”和“非鎖定”兩種狀態(tài)
  • 同一個(gè)線程獲取鎖后,如果在釋放鎖之前再次獲取鎖會(huì)導(dǎo)致當(dāng)前線程阻塞,除非有另外的線程來(lái)釋放鎖,如果只有一個(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ì)于一些可能造成死鎖的情況,可以考慮使用遞歸鎖來(lái)解決
  • 遞歸鎖和普通鎖的差別在于加入了“所屬線程”和“遞歸等級(jí)”的概念
  • 釋放鎖必須有獲取鎖的線程來(lái)進(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)行,但是通過(guò)信號(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)文章

最新評(píng)論

三门县| 南皮县| 嘉定区| 濉溪县| 乌审旗| 义马市| 扬中市| 屏南县| 弋阳县| 吉木乃县| 永丰县| 图们市| 清远市| 驻马店市| 东阿县| 修文县| 潢川县| 安丘市| 慈溪市| 公主岭市| 搜索| 乐都县| 长垣县| 嘉祥县| 会东县| 苏尼特左旗| 施秉县| 福州市| 黔西县| 南丰县| 那坡县| 堆龙德庆县| 探索| 正安县| 建昌县| 教育| 昌平区| 孟州市| 九寨沟县| 龙陵县| 开江县|