抢票小程序
我们可以利用所学的内容来写一个简易的抢票小程序
from multiprocessing import Process import json,time,os def search(): time.sleep(1) # 模拟网络io with open('db.txt',mode='rt',encoding='utf-8') as f: res = json.load(f) print(f'还剩{res["count"]}') def get(): with open('db.txt',mode='rt',encoding='utf-8') as f: res = json.load(f) # print(f'还剩{res["count"]}') time.sleep(1) # 模拟网络io if res['count'] > 0: res['count'] -= 1 with open('db.txt',mode='wt',encoding='utf-8') as f: json.dump(res,f) time.sleep(1.5) # 模拟网络io print(f'进程{os.getpid()} 抢票成功') else: print('票已经售空啦!!!!!!!!!!!') def task(): search() get() if __name__ == '__main__': for i in range(15): p = Process(target=task) p.start() p.join()
db.txt {"count": 20}
抢票小程序(优化)
之前的抢票小程序看似完善但实际上在数据的安全方面依旧存在着问题,因此我们可以使用进程锁来实现优化
进程锁
进程锁的本质则是把锁住的代码变成串行,它可以用lock.acquire和lock.release来实现
lock.acquire:锁住
lock.release:释放锁头
优化的抢票小程序
from multiprocessing import Process,Lock import json,time,os def search(): time.sleep(1) # 模拟网络io with open('db.txt',mode='rt',encoding='utf-8') as f: res = json.load(f) print(f'还剩{res["count"]}') def get(): with open('db.txt',mode='rt',encoding='utf-8') as f: res = json.load(f) # print(f'还剩{res["count"]}') time.sleep(1) # 模拟网络io if res['count'] > 0: res['count'] -= 1 with open('db.txt',mode='wt',encoding='utf-8') as f: json.dump(res,f) print(f'进程{os.getpid()} 抢票成功') time.sleep(1.5) # 模拟网络io else: print('票已经售空啦!!!!!!!!!!!') def task(lock): search() # 锁住 lock.acquire() get() lock.release() # 释放锁头 if __name__ == '__main__': lock = Lock() # 写在主进程是为了让子进程拿到同一把锁. for i in range(15): p = Process(target=task,args=(lock,)) p.start() # p.join() # 进程锁 是把锁住的代码变成了串行 # join 是把所有的子进程变成了串行 # 为了保证数据的安全,串行牺牲掉效率.