import threading
import random
import datetime
"""
模拟3天的简易仓库存货和出货运营模式:
每天售卖电脑时检查电脑仓库存储量是否小于100,小于的话去进行补货,随机生成一个补货单;
大于等于100时,去进行售卖,随机生成用户订单,进行售卖。
每天营业结束时,需要统计一天售卖的总金额和总数量
"""
SAFETY_STOCK = 100 # 安全库存
SALE_TIMES = 50 # 每天最大销售次数,超过就结束
PRICE = 100 # 商品单价
stock = 90 # 当前库存
saled_times = 0 # 每天销售次数
total_sale_amount = 0 # 每天销售总金额
total_sale_count = 0 # 每天销售总数额
lock = threading.Lock()
class Producer(threading.Thread): # threading.Thread!!!
def run(self):
global stock
global saled_times
while True:
lock.acquire()
if stock < SAFETY_STOCK:
# 补货,随机生成一个补货单,数量处理
tmp = stock
stock_in = random.randint(1, 10)
stock += stock_in
print("%s 当前库存:%s,应该生产,随机生成一个补货单: %s,补货:%d,库存为:%d"
% (threading.current_thread(), tmp, create_order_id(), stock_in, stock))
# lock.release() # 放在这儿不行,为啥?
if saled_times >= SALE_TIMES:
lock.release()
break
lock.release()
class Consumer(threading.Thread):
def run(self):
global stock
global total_sale_amount
global total_sale_count
global saled_times
while True:
lock.acquire()
if stock >= SAFETY_STOCK:
# 销售,随机生成用户订单,进行售卖
tmp = stock
stock_out = random.randint(1, 10)
sale_amount = stock_out * PRICE
stock -= stock_out
total_sale_amount += sale_amount
total_sale_count += stock_out
saled_times += 1
print("%s 当前库存:%s,应该销售,随机生成一个销售单: %s,销售:%d,销售额:%d,库存为:%d"
% (threading.current_thread(), tmp, create_order_id(), total_sale_count, total_sale_amount, stock))
if saled_times >= SALE_TIMES:
# print("当天营业额:%s, 总销售数量:%s" % (total_sale_amount, total_sale_count)) # 放在这儿,每个线程都会执行一次
lock.release()
break
lock.release()
def create_order_id():
time_stamp = '{0:%Y%m%d%H%M%S}'.format(datetime.datetime.now())
order_id = time_stamp + str(random.randint(1000, 9999))
return order_id
def main():
# create 5 producers
for i in range(5):
p = Producer(name="生产者%d" % i)
p.start()
# create 3 consumers
for j in range(3):
c = Consumer(name="消费者%d" % j)
c.start()
p.join()
c.join()
# 每天营业结束时,需要统计一天售卖的总金额和总数量
print("当天销售:%d, 销售额:%d" % (total_sale_count, total_sale_amount))
if __name__ == '__main__':
for i in range(1, 4):
saled_times = 0
print("\nDay %d" % i)
main()
"""
模拟3天的简易仓库存货和出货运营模式:
每天售卖电脑时检查电脑仓库存储量是否小于100,小于的话去进行补货,随机生成一个补货单;
大于等于100时,去进行售卖,随机生成用户订单,进行售卖。
每天营业结束时,需要统计一天售卖的总金额和总数量
condition版本模式
"""
SAFETY_STOCK = 100 # 安全库存
SALE_TIMES = 50 # 每天最大销售次数,超过就结束
PRICE = 100 # 商品单价
stock = 90 # 当前库存
saled_times = 0 # 每天销售次数
total_sale_amount = 0 # 每天销售总金额
total_sale_count = 0 # 每天销售总数额
conditon = threading.Condition()
class Producer(threading.Thread): # threading.Thread!!!
def run(self):
global stock
global saled_times
while True:
conditon.acquire()
if stock > SAFETY_STOCK:
conditon.wait()
else:
# 补货,随机生成一个补货单,数量处理
tmp = stock
stock_in = random.randint(1, 10)
stock += stock_in
print("%s 当前库存:%s,应该生产,随机生成一个补货单: %s,补货:%d,库存为:%d"
% (threading.current_thread(), tmp, "123", stock_in, stock))
# lock.release() # 放在这儿不行,为啥?
conditon.notify_all()
if saled_times >= SALE_TIMES:
conditon.release()
break
conditon.release()
class Consumer(threading.Thread):
def run(self):
global stock
global total_sale_amount
global total_sale_count
global saled_times
while True:
conditon.acquire()
if stock < SAFETY_STOCK:
conditon.wait()
else:
# 销售,随机生成用户订单,进行售卖
tmp = stock
stock_out = random.randint(1, 10)
sale_amount = stock_out * PRICE
stock -= stock_out
total_sale_amount += sale_amount
total_sale_count += stock_out
saled_times += 1
print("%s 当前库存:%s,应该销售,随机生成一个销售单: %s,销售:%d,销售额:%d,库存为:%d"
% (threading.current_thread(), tmp, "123", total_sale_count, total_sale_amount, stock))
conditon.notify_all()
if saled_times >= SALE_TIMES:
# print("当天营业额:%s, 总销售数量:%s" % (total_sale_amount, total_sale_count)) # 放在这儿,每个线程都会执行一次
conditon.release()
break
conditon.release()
def main():
# create 5 producers
for i in range(5):
p = Producer(name="生产者%d" % i)
p.start()
# create 3 consumers
for j in range(3):
c = Consumer(name="消费者%d" % j)
c.start()
p.join()
c.join()
# 每天营业结束时,需要统计一天售卖的总金额和总数量
print("当天销售:%d, 销售额:%d" % (total_sale_count, total_sale_amount))
if __name__ == '__main__':
# main()
for i in range(1, 4):
saled_times = 0
print("\nDay %d" % i)
main()
多线程_生产者_消费者_锁Lock_Condition
最新推荐文章于 2022-02-20 01:20:34 发布