Condition synchronization and condition variable synchronization almost mean, but less lock function.
Event=threading. Event (): Conditional Environment object with an initial value of false;
Event.isset (): Returns the status value of the event;
Event.wait (): If Event.isset () ==false will block the thread;
Event.set (): Sets the status value of event to true, all the threads of the blocking pool are activated into a ready state, waiting for the operating system to dispatch;
Event.clear (): The status value of recovery event is false.
ImportThreading,timeclassBoss (Threading. Thread):defRun (self):Print("BOSS: Everyone has to work overtime tonight. ") Event.set () Time.sleep (5) Print("BOSS: You can get off work now. ") Event.set ()classWorker (Threading. Thread):defRun (self): event.wait ()Print("Worker: Hey ... Commiseration Ah! ") Time.sleep (0.25) Event.clear () event.wait ()Print("worker:yeah!")if __name__=="__main__": Event=Threading. Event () Threads=[] forIinchRange (5): Threads.append (Worker ()) Threads.append (Boss ()) forTinchThreads:t.start () forTinchThreads:t.join ()
Python threading Synchronization condition (Event)