在資源設計中,所有的資源從配置檔案中讀出後成為例項儲存,通常情況下資源的名字是唯一的,因此可以通過資源名作為的鎖的關鍵字,然後使用乙個結構體來儲存相應資源的鎖資訊,並使用訊號量來標識鎖狀態
假設執行緒a對該資源進行操作,重置了訊號量,當執行緒b也需要使用該資源並對其進行加鎖的時候,就會因為該資源所對應的訊號量而阻塞住執行緒,直到該訊號量被執行緒a置位,代表執行緒a對該資源的占用結束,執行緒b才能繼續
from core.result.logger import logger
from core.utilities.time import get_local_time
from threading import event, thread
import time
class
resourceislocked
(exception)
:def
__init__
(self, resource, event, timeout)
:super()
.__init__(f"resource is locked by and not released in s"
)class
invalidlockoperation
(exception)
:pass
class
resourcelockpool
:"""
資源鎖池
"""def__init__
(self, log=
none):
self.log = log if log is
notnone
else logger.register(
"resourcelockpool"
, default_level=
"info"
) self.resource =
dict()
deflock
(self, resource, event, timeout=60)
:"""
鎖定資源
"""if resource.name in self.resource:
lock = self.resource[resource.name]
['lock'
] event_name = self.resource[resource.name]
['event']if
not lock.wait(timeout)
:raise resourceislocked(resource.name, event_name, timeout)
self.log.info(f"lock : time: "
) self.resource[resource.name]
=def
release
(self, resource, event)
:"""
釋放資源
"""if resource.name in self.resource:
if self.resource[resource.name]
['event'
]== event:
self.log.info(f"release lock for "
) self.resource[resource.name]
['lock'].
set(
) self.resource.pop(resource.name)
else
:raise invalidlockoperation(
f" is locked by "
)else
:raise invalidlockoperation(f' is not locked'
)if __name__ ==
"__main__"
:class
testresource
:"""
代表測試資源類
"""def__init__
(self, name)
: self.name = name
log = logger.register(
"testlog"
) pool = resourcelockpool(log)
device1 = testresource(
'device1'
)"""
測試:定義兩個方法,用來在兩個執行緒中對同乙個資源進行訪問
"""deftest_method1()
: pool.lock(device1,
"event1"
) log.info(
"method1 start"
) time.sleep(10)
pool.release(device1,
"event1"
) log.info(
"method1 stop"
)def
test_method2()
: pool.lock(device1,
"event2"
) log.info(
"method2 start"
) time.sleep(10)
pool.release(device1,
"event2"
) log.info(
"method2 stop"
) thread1 = thread(target=test_method1)
thread2 = thread(target=test_method2)
threads =
[thread1, thread2]
for t in threads:
t.start(
)for t in threads:
t.join(
)
[2020-08-01 11:20:18,258]
[testlog]--(line:33), [info]: lock device1: time: august 01, 20 11:20:18
[2020-08-01 11:20:18,258]
[testlog]--(line:70), [info]: method1 start
[2020-08-01 11:20:28,271]
[testlog]--(line:46), [info]: release lock for device1
[2020-08-01 11:20:28,271]
[testlog]--(line:73), [info]: method1 stop
[2020-08-01 11:20:28,271]
[testlog]--(line:33), [info]: lock device1: time: august 01, 20 11:20:28
[2020-08-01 11:20:28,271]
[testlog]--(line:78), [info]: method2 start
[2020-08-01 11:20:38,280]
[testlog]--(line:46), [info]: release lock for device1
[2020-08-01 11:20:38,280]
[testlog]--(line:81), [info]: method2 stop
自動化測試平台化 v1 0 0 物件導向
encoding utf 8 物件導向第一大特徵 封裝 基於python3 class cellphone 手機類 def init self,cell phone number self.cell phone number cell phone number self.battery percen...
自動化測試 web自動化測試
自動化 由機器裝置代替人為完成制定目標的過程 優點 提高工作效率 減少勞動力 產品規格同一標準 批量生產 自動化測試 讓程式代替人為去驗證程式功能的過程,即在預設條件下執行程式系統 流程確定 搭建自動化框架 編寫測試用例,將其轉化為soupui 介面 自動化測試指令碼 執行自動化測試指令碼 輸出執行...
自動化測試平台的探索
只做了基本的幾個控制項,在文字框內輸入要生成的檔案數量,點選生成後在相應的目錄下生成對應的檔案 對應的 也很簡單,就乙個from表單,如下 1 doctype html 2 html lang en 3 head 4 meta charset utf 8 5 title 測試檔案生成頁 生成 sty...