#join
from multiprocessing import Process,Lock
import time
import json
def search(name):
time.sleep(1)
dic = json.load(open('E:/py3/db.txt','r',encoding='utf-8'))
print('<%s> 查看剩余票数 [%s]' %(name,dic['count']))
def get(name):
#网络延迟
time.sleep(1)
#下面的流程 都是10个人并发操作了
dic = json.load(open('E:/py3/db.txt','r',encoding='utf-8'))
if dic['count'] > 0:
dic['count'] -=1
time.sleep(3)
json.dump(dic,open('E:/py3/db.txt','w',encoding='utf-8'))
print('<%s> 购票成功' %name)
else:
print('<%s> 购票失败' %name)
def task(name,mutex):
search(name)
# mutex.acquire()
get(name)
# mutex.release()
if __name__ == '__main__':
mutex=Lock()
for i in range(10):
p=Process(target=task,args=('路人%s' %i,mutex))
p.start()
#可以把并发编程串行,但是这个进程整个代码都变成串行
p.join()
#互斥锁 是局部的
from multiprocessing import Process,Lock
import time
import json
def search(name):
time.sleep(1)
dic = json.load(open('E:/py3/db.txt','r',encoding='utf-8'))
print('<%s> 查看剩余票数 [%s]' %(name,dic['count']))
def get(name):
#网络延迟
time.sleep(1)
#下面的流程 都是10个人并发操作了
dic = json.load(open('E:/py3/db.txt','r',encoding='utf-8'))
if dic['count'] > 0:
dic['count'] -=1
time.sleep(3)
json.dump(dic,open('E:/py3/db.txt','w',encoding='utf-8'))
print('<%s> 购票成功' %name)
else:
print('<%s> 购票失败' %name)
def task(name,mutex):
search(name)
mutex.acquire()
get(name)
mutex.release()
if __name__ == '__main__':
mutex=Lock()
for i in range(10):
p=Process(target=task,args=('路人%s' %i,mutex))
p.start()
#可以把并发编程串行,但是这个进程整个代码都变成串行
# p.join()