程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Master Wus Python bakery] day 3

編輯:Python

Preface : Master Wu, who can only stay at home on the national day, is very boring , Decided to open a Python The bakery passed the time . Every day after that , Master Wu will use a piece of code to simply realize the function of selling bread , And solve the problems exposed the day before .

The baker did not wait for the customers to come to make bread , I always finish the bread , When the customer comes, he takes the bread and goes away .

"""python When the baker sells the bread prepared in advance , And prepare new bread at the same time ."""
import time
import threading
TOTAL = 0
END_FLAG = False
def preproduce():
""" Bread prepared in advance ."""
global TOTAL
TOTAL += 10
def producer():
""" Making bread ."""
num = 0
global TOTAL
global END_FLAG
while True:
time.sleep(1)
TOTAL += 1
num += 1
if num >= 10:
# Shifu did it 10 A loaf of bread is coming off work .
END_FLAG = True # close the door , Customer consumption should also be stopped
print('Close.')
break
def consumer():
""" consumer , You have to say something to buy bread ."""
global TOTAL
global END_FLAG
while not END_FLAG:
if TOTAL > 0:
print("I am so happy.")
TOTAL -= 1
else:
time.sleep(2)
print("I am waiting!")
def run():
preproduce()
p = threading.Thread(target=producer)
c = threading.Thread(target=consumer)
p.setDaemon(True)
p.start()
c.start()
p.join()
c.join()
if __name__ == "__main__":
run()

The baker complained that he was too tired to work alone , Master Wu decided to ask two more masters to help .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved