抢票小程序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抢票成功
01-19 17:29