模拟生产,入库,销售场景
假定企业自产,自存,自销。将工厂生产的产品不定时的运到仓库,与此同时,仓库
货物需要运到商场销售。请编写一个程序模拟这个过程,(主要是对仓库的存取)。
- 仓库可存量,可以设置为一个常量,比如max = 10
- 仓库满的时候不能向仓库存入货物
- 仓库空的时候不能向商场提供货物
- 写多线程表达货物的存储和提取,而这两个操作是同时进行的
- 不能出现先存满再取完或者先取完再存满的情况
解答:
from threading import Thread
import time
from random import randint
#仓库 规定列表中最多存入10个内容
ware = []
#生产者函数
def produce(name):
count = 0
while True:
print("%s正在生产中…"%name)
time.sleep(2) #每隔2秒生产一个
if len(ware) >= 10:
print(“仓库满了…”)
time.sleep(4)
else:
ware.append(“good-%d”%count)#将货物存入仓库
print(“Produce%s做好了一个商品”%name)
count +=1
#消费者函数
def cnsumer(name):
while True:
time.sleep(randint(1,8))
if len(ware) <= 0 :
print(“断货了…”)
time.sleep