import json,time
# 读写数据库中的票数
def wr_info(sign,dic=None):
if sign == "r":
with open("ticket",mode="r",encoding="utf-8") as fp:
dic = json.load(fp)
return dic
elif sign == "w":
with open("ticket",mode="w",encoding="utf-8") as fp:
json.dump(dic,fp)
# res = wr_info("r")
# print(res)
# dic = {"count":0}
# wr_info("w",dic)
# 抢票方法
def get_ticket(person):
# 获取数据库中实际的票数
dic = wr_info("r")
print(dic)
# 模拟一下网络延迟
time.sleep(0.5)
# 判断票数
if dic["count"] > 0 :
print("%s抢到票了" % (person))
dic["count"] -= 1
wr_info("w",dic)
else:
print("%s没有抢到这张票" % (person))
def run(person,lock):
# 查看剩余票数
dic = wr_info("r")
print("%s 查询票数: %s" % (person , dic["count"]) )
# 上锁
lock.acquire()
# 开始抢票
get_ticket(person)
lock.release()
if __name__ == "__main__":
lock = Lock()
lst = ["科比","麦迪","高峰","姚明","邓肯","成龙","刘亦菲"]
for i in lst:
p = Process(target=run,args=(i,lock))
p.start()
12306抢票软件
猜你喜欢
转载自blog.csdn.net/qq_45066628/article/details/113879167
今日推荐
周排行