标签:import start open rgs target lse art pytho env
#!/usr/bin/env python #!--*--coding:utf-8 --*-- #!@Time :2018/7/14 17:33 #!@Author TrueNewBee #锁 # 火车票 import json import time from multiprocessing import Process from multiprocessing import Lock def show(i): with open(‘ticket‘) as f: dic = json.load(f) print(‘余票: %s‘%dic[‘ticket‘]) def buy_ticket(i,lock): lock.acquire() #拿钥匙进门 with open(‘ticket‘) as f: dic = json.load(f) time.sleep(0.1) if dic[‘ticket‘] > 0 : dic[‘ticket‘] -= 1 print(‘\033[32m%s买到票了\033[0m‘%i) else: print(‘\033[31m%s没买到票\033[0m‘%i) time.sleep(0.1) with open(‘ticket‘,‘w‘) as f: json.dump(dic,f) lock.release() # 还钥匙 if __name__ == ‘__main__‘: for i in range(10): p = Process(target=show,args=(i,)) p.start() lock = Lock() for i in range(10): p = Process(target=buy_ticket, args=(i,lock)) p.start()
天真热,,天干物燥,! 要安安静静多看书!
标签:import start open rgs target lse art pytho env
原文地址:https://www.cnblogs.com/zhen1996/p/9314213.html