A "flag" is defined globally, and if the "flag" value is False, then it blocks when the program executes the Event.wait method, and if the "flag" value is true, then the Event.wait method is no longer blocked.
Clear: Set "Flag" to False
Set: Sets "Flag" to True
With threading. Event implements inter-thread communication, using threading. Event allows a thread to wait for notifications from other threads, and we pass this event to the thread object,
Event has a flag built in by default and the initial value is false. Once the thread enters the waiting state through the wait () method until another thread calls the event's set () method to set the built-in flag to true, the event notifies all waiting-state threads to resume running.
Importthreading,timeevent=Threading. Event ()defAA ():Print('begin') event.wait ()Print('End') F=threading. Thread (target=aa) Event.clear ()#Although the event initial default value is False, it is customary to #or set it up .F.start () time.sleep (10)#threads are blocked during this timeEvent.set ()
Python thread Event