#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)