Python multithreaded Asynchronous simple small chestnut (bun Wars)

Source: Internet
Author: User

#coding: Utf-8import threadingimport timedef producer ():     print u ' tantianran:  and other people to buy steamed buns ... '     event.wait ()   #一开始为阻塞, if the flag bit is detected to be set to True (the flag bit is flase by default), Continue to execute     event.clear ()   #将标志位设置回flase     print u ' tantianran:  production of steamed buns ... '     time.sleep (5)     print u ' Tantianran :  urged your sister! Your buns are ready ... '     event.set ()   #  set flag bit to True, notify thread T2, bun Ready Def consumer ():     print u ' tanzhenxing:  to buy buns ... '     event.set ()  # Set flag bit to True to notify T1 thread to buy buns     time.sleep (2)     while True:         if event.isset ():             print u ' tanzhenxing:  bought steamed buns, steamed buns are really delicious ... Thank you! '             break        else:             print u ' tanzhenxing:  buns and not a good horse? '             time.sleep (2) if __name__ ==   ' __main__ ':     event = threading. Event ()     t1 = threading. Thread (Target=producer)     t2 = threading. Thread (Target=consumer)     t1.start ()     t2.start ()


Execution Result:

/usr/bin/python2.7/home/toby/pycharmprojects/test-20170419/01.py
Tantianran: Wait for people to buy steamed buns ...
Tanzhenxing: To buy steamed buns ...
Tantianran: Production of steamed buns ...
Tanzhenxing: Buns and martinis are not doing well?
Tanzhenxing: Buns and martinis are not doing well?
Tantianran: Hurry up your sister! Your buns are ready ...
tanzhenxing: Buy steamed buns, steamed buns are really delicious ... Thank you!

This article is from the "Operation and maintenance of AC Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1918600

Python multithreaded Asynchronous simple small chestnut (bun Wars)

Related Article

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.