網站首頁 編程語言 正文
在多線程中使用lock可以讓多個線程在共享資源的時候不會“亂”,例如,創建多個線程,每個線程都往空列表l中添加一個數字并打印當前的列表l,如果不加鎖,就可能會這樣:
# encoding=utf8 import threading import time lock = threading.Lock() l = [] def test1(n): lock.acquire() l.append(n) print l lock.release() def test(n): l.append(n) print l def main(): for i in xrange(0, 10): th = threading.Thread(target=test, args=(i, )) th.start() if __name__ == '__main__': main()
運行結果:
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3][
0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4[, 05, , 16, , 27, ]3
, 4, 5, 6[, 07, , 18, ]2
, 3, 4, [50, , 61, , 72, , 83, , 94],?
5, 6, 7, 8, 9]
因為每個線程都在同時往l中添加一個數字(當前每個線程運行的是test函數),然后又可能在同時打印l,所以最后的結果看起來會有些“混亂”。
下面讓每個線程調用“test1”函數,看看結果如何:
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
現在看起來就好多了,因為test1中每次像l中添加數字并打印之前,都先加了一把“鎖”,這樣就可以保證每次只有一個線程可以往l中添加數字,而不是同時往l里添加數字。
通過上面的結果比較可以知道,當多線程中需要“獨占資源”的時候,要使用鎖來控制,防止多個線程同時占用資源而出現其他異常。
使用鎖的時候就調用acquire()方法,以此告訴其他線程,我正在占用該資源,你們要等會;待使用資源后需要釋放資源的時候就調用release()方法,告訴其他線程,我已經完成使用該資源了,其他人可以過來使用了。
python threading Lock
這篇文章主要是通過代碼說明:
-
threading.Lock()
不影響multiprocessing
- .
threading.Lock()
影響threading
.
代碼如下:
import threading import time from multiprocessing import Pool _lock = threading.Lock() def small_func(value): """ 添加線程鎖 :param value: :return: """ print(value) with _lock: time.sleep(5) return value def no_small_func(value): """ 沒有線程鎖 :param value: :return: """ print(value) # with _lock: time.sleep(5) return value def main(): """ multiprocessing 是基于進程的,因此線程鎖對其不影響, :return: """ st = time.time() p = Pool(processes=4) value = p.map(func=small_func, iterable=range(4)) et = time.time() print(f"all use time: {et - st}") print(value) def main2(): """ threading 受到 線程鎖 影響 :return: """ st = time.time() thread_list = [] for temp_value in range(4): t = threading.Thread(target=small_func, args=(temp_value,)) t.start() thread_list.append(t) for i in thread_list: i.join() et = time.time() print(f"all use time: {et - st}") # print(value) def main3(): st = time.time() thread_list = [] res = [] for temp_value in range(4): # 不加線程鎖就行了 t = threading.Thread(target=no_small_func, args=(temp_value,)) t.start() thread_list.append(t) for i in thread_list: v = i.join() res.append(v) et = time.time() print(f"all use time: {et - st}") print(res) if __name__ == '__main__': # main() # main2() main3()
原文鏈接:https://blog.csdn.net/u012067766/article/details/79733801
相關推薦
- 2022-12-12 pycharm?console?打印中文為亂碼問題及解決_python
- 2021-12-02 C++11?constexpr使用詳解_C 語言
- 2023-04-02 go?MethodByName()不能獲取私有方法的解決_Golang
- 2023-02-05 Python實現自定義包的實例詳解_python
- 2022-10-20 Android開發之Gradle?進階Tasks深入了解_Android
- 2024-04-05 idea使用docker生成鏡像(打包鏡像,導入鏡像,導出鏡像)
- 2023-07-07 什么是依賴注入?可以通過多少種方式完成依賴注入?
- 2022-04-20 python錯誤提示:Errno?2]?No?such?file?or?directory的解決方法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支