日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Python并行编程(八):with语法

發布時間:2023/11/27 生活经验 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python并行编程(八):with语法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、基本概念

? ? ? 當有兩個相關的操作需要在一部分代碼塊前后分別執行的時候,可以使用with語法自動完成。同時,使用with語法可以在特定的地方分配和釋放資源,因此,with語法也叫作"上下文管理器"。在threading模快中,所有帶有acquire()方法和release()方法的對象都可以使用上下文管理器。主要用于代碼塊的收尾工作。

? ? ? 也就是說,下面的對象可以使用with語法:

? ? ? ? ? ? Lock、RLock、Condition、Semaphore

?

2、測試用例

# coding : utf-8import threading
import logginglogging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s',)def threading_with(statement):with statement:logging.debug('%s acquired via with' % statement)def threading_not_with(statement):statement.acquire()try:logging.debug('%s acquired directly' % statement)finally:statement.release()if __name__ == '__main__':lock = threading.Lock()rlock = threading.RLock()condition = threading.Condition()mutex = threading.Semaphore(1)threading_synchronization_list = [lock, rlock, condition, mutex]for statement in threading_synchronization_list:t1 = threading.Thread(target=threading_with, args=(statement,))t2 = threading.Thread(target=threading_not_with, args=(statement,))t1.start()t2.start()t1.join()t2.join()

?

轉載于:https://www.cnblogs.com/dukuan/p/9798036.html

總結

以上是生活随笔為你收集整理的Python并行编程(八):with语法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。