抢票小程序
发布时间:2020-12-20 10:44:30 所属栏目:Python 来源:网络整理
导读:from multiprocessing import Processimport json,time,osdef search(): time.sleep(1) with open('db.txt','rt',encoding='utf8') as f: res=json.load(f) print(f'还剩{res["count"]}')def get(): with open('db.txt',encoding='utf8')as f: res=json.load
from multiprocessing import Process
import json,time,os
def search():
time.sleep(1)
with open('db.txt','rt',encoding='utf8') as f:
res=json.load(f)
print(f'还剩{res["count"]}')
def get():
with open('db.txt',encoding='utf8')as f:
res=json.load(f)
time.sleep(1)
if res['count']>0:
res['count']-=1
with open('db.txt','wt',encoding='utf8')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(10):
p=Process(target=task)
p.start()
p.join()
# 为了保证数据的安全,要牺牲掉效率.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
