抢票小程序

抢票小程序1

为了保证数据的安全,因此要牺牲掉效率,效率慢

#db.txt
{"count": 2}
from multiprocessing import Process
import json,time,os
def search():
    time.sleep(1)#模拟网络信号IO
    with open('db.txt',mode='rt',encoding='utf8') as f:
        res = json.load(f)
        print(f'还剩余{res["count"]}')
def get():
    with open('db.txt',mode='rt',encoding='utf8') as f:
        res = json.load(f)
    time.sleep(1)
    if res['count']>0:
        res['count']-=1
        with open('db.txt',mode='wt',encoding='utf8') as f:
            json.dump(res,f)
            time.sleep(1.5)
            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()
######################
还剩余2
进程4904抢票成功
还剩余1
进程16108抢票成功
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!
还剩余0
票已经售空啦!!!!!!!

优化抢票小程序

from multiprocessing import Process,Lock
import json, time, os


def search():
    time.sleep(1)  # 模拟网络io
    with open('db.txt', mode='rt', encoding='utf8') as f:
        res = json.load(f)
        print(f'还剩余{res["count"]}')


def get():
    with open('db.txt', mode='rt', encoding='utf8') as f:
        res = json.load(f)
    time.sleep(1)
    if res['count'] > 0:
        res['count'] -= 1
        with open('db.txt', mode='wt', encoding='utf8') as f:
            json.dump(res, f)
            time.sleep(1.5)
            print(f'{os.getpid()}抢票成功')
    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()
    #进程锁,是把锁住的代码变成串行
    #jion是把所有的子进程变成串行
#######################
还剩余2
7964抢票成功
还剩余1
11164抢票成功
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!
还剩余0
票已经售空!!!!!!!!!!!!

优化抢票小程序2

from multiprocessing import Process,Lock
import json, time, os


def search():
    time.sleep(1)  # 模拟网络io
    with open('db.txt', mode='rt', encoding='utf8') as f:
        res = json.load(f)
        return res['count']


def get():
    with open('db.txt', mode='rt', encoding='utf8') as f:
        res = json.load(f)
        print(f'还剩余{res["count"]}')
    time.sleep(1)
    if res['count'] > 0:
        res['count'] -= 1
        with open('db.txt', mode='wt', encoding='utf8') as f:
            json.dump(res, f)
            time.sleep(1.5)
            print(f'{os.getpid()}抢票成功')
    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()
    #进程锁,是把锁住的代码变成串行
    #jion是把所有的子进程变成串行
        res = search()
        if res==0:
            p.terminate()
            break
 #################
还剩余2
4356抢票成功
还剩余1
15936抢票成功

转载于:https://www.cnblogs.com/SkyOceanchen/p/11537595.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值