码迷,mamicode.com
首页 > 微信 > 详细

抢票小程序

时间:2019-09-17 22:47:48      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:print   ESS   python   rgs   targe   from   ssi   ase   search   

抢票小程序

我们可以利用所学的内容来写一个简易的抢票小程序

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 是把所有的子进程变成了串行


# 为了保证数据的安全,串行牺牲掉效率.

抢票小程序

标签:print   ESS   python   rgs   targe   from   ssi   ase   search   

原文地址:https://www.cnblogs.com/jie9527-/p/11537470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!