• 009 模拟一个简单抢票小程序代码


    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()
    
    
    # 为了保证数据的安全,要牺牲掉效率.
    
  • 相关阅读:
    CMDB表结构设计
    mysql数据库密码
    web之提取状态码
    ELK的启动脚本
    ELK的轻量级搭建使用
    liunx安装JDK
    liunx的urandom生成随机字符
    vim快捷键
    升级系统的python程序
    Mysql的进阶小记
  • 原文地址:https://www.cnblogs.com/abdm-989/p/11512935.html
Copyright © 2020-2023  润新知