簡體   English   中英

在任何情況下,threading.Event 的 set() 或 clear() 函數是否會失敗

[英]Can threading.Event's set() or clear() function fail in any case

是否存在threading.Eventset()clear()會失敗的情況,即。 在沒有clear()等的情況下再次調用set()

現在我在所有set()clear()調用周圍添加try-except塊。

Python27 文檔中,沒有具體說明。

我問這個是因為從直覺上講,如果您設置了一個已經設置的布爾標志或清除它,則引發異常是沒有意義的。

TLDR:沒有。

Event的真實性是一個簡單的布爾值,由鎖保護。

class Event:
    """Class implementing event objects.

    Events manage a flag that can be set to true with the set() method and reset
    to false with the clear() method. The wait() method blocks until the flag is
    true.  The flag is initially false.
    """
    def __init__(self):
        self._cond = Condition(Lock())
        self._flag = False

設置和清除只是獲取鎖定並設置值。 不涉及切換,也不檢查先前的值。 沒有顯式拋出異常 - 您當然可以獲得通用異常,例如KeyboardInterrupt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM