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()
# 为了保证数据的安全,要牺牲掉效率.
009 模拟一个简单抢票小程序代码
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:056 文件修改的两种方式
下一篇:055 文件的高级应用
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
用这个抢票神器,一抢一个准!
牛X,试用了下GitHub上22万Star的第一抢票神器,3秒钟抢到!
github python fish 公众号 微信