One: Thread event function
Python provides the event object for inter-thread communication, which is a signal flag set by the thread, and if the signal flag bit is true, other threads wait until the signal ends.
The event object implements a simple threading mechanism that provides a set of signals, clear signals, waits, and so on to enable communication between threads.
Official explanation
An event is a simple synchronization object, and an event represents an internal flag and thread. You can wait for the flag to set, or to set or clear the flag itself.
II: Event Use
1) event = Threading. Event () Declaration event Instance
2) event.wait (). Thread set Wait flag
3) Event.set () If the flag is set, wait for the method to take no action
4) Event.clear () If the flag is cleared, the wait will block until it is set again.
Any number of threads can wait for the same event.
Three: Sample code
Through the event to realize the interaction between two or more threads, here is an example of a traffic light, that is, start a thread to do traffic lights, generate a few threads to do the vehicle, the vehicle driving by red light stop, Green Line rules.
Python Basic Learning Log day9--thread event