BetterEvent¶
-
class
privex.helpers.thread.
BetterEvent
(wait_on: str = 'set', name: str = None, default: bool = False, notify_set=True, notify_clear=True)[source]¶ BetterEvent
(aliasInvertibleEvent
) is a more flexible version ofthreading.Event
, which adds many new capabilities / features on top ofthreading.Event
:The
wait_on
constructor parameter allows you to choose what flag states that the standardwait()
will trigger upon:'set'
- the default - works likethreading.Event
,wait()
only triggers when the event is in the “set” state, i.e._flag
isTrue
'clear'
- opposite of the default - works opposite tothreading.Event
,wait()
only triggers when the event is in the “clear” state, i.e._flag
isFalse
'both'
- In theboth
setting,wait()
will simply wait until_flag
is changed, whether fromset
toclear
, orclear
toset
. This wait_on setting only works as long asnotify_set
andnotify_clear
are set toTrue
The
default
constructor parameter allows you to choose whether the event starts as “cleared” (False
- default), or “set” (True
), which is useful when using some of the alternativewait_on
settings.New
fail
parameter forwait()
,wait_set()
andwait_clear()
- when this is set toTrue
, the method will raiseEventWaitTimeout
when the timeout is hit, instead of just returningFalse
.New
wait_set()
method, this works like the classicthreading.Event
wait
method - it’s only triggered when_flag
is set toTrue
(set) - no matter whatwait_on
setting is active.New
wait_clear()
method, this works opposite to the classicthreading.Event
wait
method - it’s only triggered when_flag
is set toFalse
(cleared) - no matter whatwait_on
setting is active.
Example Usage
Below is a very simple thread class using
SafeLoopThread
which uses aBetterEvent
so we can signal when it can start running, and when it’s allowed to restart itself:>>> from privex.helpers import BetterEvent, SafeLoopThread >>> >>> class MyThread(SafeLoopThread): ... def __init__(self, *args, trig, **kwargs): ... self.trig = trig ... super().__init__(*args, **kwargs) ... def loop(self): ... print("Waiting for trig to become set before doing stuff...") ... self.trig.wait() # Same behaviour as threading.Event.wait - waits for trig.set() ... print("trig is set. doing stuff...") ... print("finished doing stuff.") ... print("Waiting for trig to become clear before restarting loop...") ... self.trig.wait_clear() # Unlike threading.Event, BetterEvent allows waiting for the "clear" signal ... >>> evt = BetterEvent(name='My Event') >>> t = MyThread(trig=evt) >>> t.start() Waiting for trig to become set before doing stuff... >>> evt.set() # We flip evt (trig) to "set", which notifies MyThread it can proceed. trig is set. doing stuff... finished doing stuff. Waiting for trig to become clear before restarting loop... >>> evt.clear() # Unlike threading.Event, we can "clear" the event, and MyThread will detect the "clear" signal instantly. Waiting for trig to become set before doing stuff... >>> evt.set() # The loop restarted. Now we can flip trig back to "set" trig is set. doing stuff... finished doing stuff. Waiting for trig to become clear before restarting loop...
-
__init__
(wait_on: str = 'set', name: str = None, default: bool = False, notify_set=True, notify_clear=True)[source] - Parameters
wait_on –
set
(default) -wait()
triggers when event is “set”.clear
-wait()
triggers when event is “clear”.both
-wait()
triggers when the event state flips from “set” to “clear” or vice versa.name (str) – An optional name to identify this event
default – The default state of the event. Either
False
for “clear”, orTrue
for “set”notify_set – Whether to notify state listeners
wait()
wait_set()
wait_clear()
whenset()
is called and the state changes from “clear” to “set”notify_clear – Whether to notify state listeners
wait()
wait_set()
wait_clear()
whenclear()
is called and the state changes from “set” to “clear”
Methods¶
Methods
|
|
|
Reset the internal flag to false. |
|
Set the internal flag to true. |
|
Multi-purpose |
|
Wait until |
|
Wait until |