Python thread Threading (iii) http://www.cnblogs.com/someoneHan/p/6213100.html A brief introduction to the event.
But if the thread intends to repeatedly notify an event over and over again. should use condition
1. Use condition should first obtain condition even if the condition enters the state of the lock
2. The thread goes into a blocking state by waiting for other threads to be notified during the execution of the thread before it starts to run down the place using the Condition.wait () method.
3. Use condition to release the lock ().
4. If condition notifies all waiting threads to continue running, you can use the Notify_all () method if you only wake one of the threads using the Notify method
1 ImportThreading2 Import Time3 4 5 classCountdown (Threading. Thread):6 def __init__(self, Startnum, condition):7Self.condition =condition8Self.startnum =Startnum9Threading. Thread.__init__(self)Ten One defRun (self): A whileSelf.startnum >0: - With self.condition: -Self.startnum-= 1 the Print('countdown Current num:', Self.startnum) - self.condition.wait () - - + classCountup (Threading. Thread): - def __init__(self, Startnum, condition): +Self.condition =condition ASelf.startnum =Startnum atThreading. Thread.__init__(self) - - defRun (self): - whileSelf.startnum < 100: - With self.condition: -Self.startnum + = 1 in Print('countup Current num:', Self.startnum) - self.condition.wait () to +Condition =Threading. Condition () -Countdown = Countdown (100, condition) the Countdown.start () *Countup =countup (0, condition) $ Countup.start ()Panax Notoginseng forIinchRange (100): - With condition: the Print('Notify') + Condition.notify_all () #如果只通知一个线程继续运行使用 condition.notify () ATime.sleep (1)
Python thread Threading (iv)