Python thread Threading (iv)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.