首页 > 微信 > 详细

抢票小程序

时间:2019-09-12 22:00:31      阅读:124      评论:0      收藏:0      [点我收藏+]
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','rt',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()
# 为了保证数据的安全,要牺牲掉效率.

抢票小程序

原文:https://www.cnblogs.com/aden668/p/11514922.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!