mutex --- mutex 模組¶
mutex 模組提供一個互斥(mutual-exclusion)原語,用以保護程式碼的臨界區段或共享資料,避免被中斷服務常式與主執行緒並行存取。
鎖的取得與釋放透過情境管理器(with 陳述式)執行,它會封鎖直到 mutex 可用為止。同時也提供一個非封鎖的 Mutex.test() 方法。
範例:
from mutex import Mutex
mtx = Mutex()
# Acquire for a critical section (blocks if already held).
with mtx:
# ... protected code, e.g. shared buffer access ...
pass
# Or try without blocking.
if mtx.test():
try:
# ... protected code ...
pass
finally:
mtx.release()
class Mutex -- mutex 物件¶
例外¶
- exception mutex.MutexException¶
OSError的子類別。當 mutex 已是未上鎖狀態時,由Mutex.release引發。