标签:macbook controls 抢票 and time arc rand style from
from multiprocessing import Process,Lock import time,json,random def search(): dic=json.load(open(‘db‘)) print(‘剩余票数%s‘ %dic[‘count‘]) def get(): dic=json.load(open(‘db‘)) time.sleep(random.random()) # 模拟读数据的网络延迟 if dic[‘count‘] >0: dic[‘count‘]-=1 time.sleep(random.random()) # 模拟写数据的网络延迟 json.dump(dic,open(‘db‘,‘w‘)) print(‘购票成功‘) else: print(‘购票失败‘) def task(lock): search() lock.acquire() get() lock.release() if __name__ == ‘__main__‘: lock = Lock() for i in range(100): # 模拟并发100个客户端抢票 p=Process(target=task,args=(lock,)) p.start()
macname@MacdeMacBook-Pro py % python3 cccccc.py
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
剩余票数2
购票成功
购票成功
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
购票失败
macname@MacdeMacBook-Pro py %
标签:macbook controls 抢票 and time arc rand style from
原文地址:https://www.cnblogs.com/sea-stream/p/14192838.html