python基础-day39

xiaoxiao2021-02-28  34

#锁 import os import time import random from multiprocessing import Lock # 锁 from multiprocessing import Process def work(n,lock): lock.acquire() print('%s: %s is running' %(n,os.getpid())) #在这里,同一进程的running和done肯定是紧挨的, #因为只有一把锁, time.sleep(random.random()) print('%s:%s is done' %(n,os.getpid())) lock.release() if __name__ == '__main__': lock = Lock() for i in range(10): p=Process(target=work,args=(i,lock)) p.start()

#锁之购票例子 import json import time import random from multiprocessing import Process, Lock def check_ticket(i): with open('ticket') as f: #仅仅查看,不需要加锁, ticket_count = json.load(f) print('person%s查询当前余票 :' % i, ticket_count['count']) def buy_ticket(i, lock): check_ticket(i) lock.acquire() #如果不能获取锁,代表当前票被占用着, with open('ticket') as f: ticket_count = json.load(f) time.sleep(random.random()) if ticket_count['count'] > 0: print('person%s购票成功' % i) ticket_count['count'] -= 1 else: print('余票不足,person%s购票失败' % i) #其实可以把下面的更改优化到上面,因为这里不涉及更改 time.sleep(random.random()) with open('ticket', 'w') as f: json.dump(ticket_count, f) #购票成功,需要把数据更新回文件,并释放保护锁, lock.release() if __name__ == '__main__': lock = Lock() for i in range(10): Process(target=buy_ticket, args=[i, lock]).start()

#你能看到这里,我暂且认为你对锁的理解OK, #锁,锁定的是一个资源 #而信号量,锁定的是一类资源,即可能有好几个这类资源,可以理解为锁+计数器 # KTV 4个房子 import time import random from multiprocessing import Process,Semaphore def ktv(i,sem): sem.acquire() #若计数器大于0,计数器-1;否则阻塞. print('person %s 进来唱歌了'%i) time.sleep(random.randint(1,5)) print('person %s 从ktv出去了'%i) sem.release() #计数器+1 if __name__ == '__main__': sem = Semaphore(4) for i in range(10): Process(target=ktv,args=(i,sem)).start()

#你是否有这样一个疑问? #锁和Event有什么区别?? #锁,锁定的是一段资源, #Event,是通过事件触发,去激活某个被阻塞的状态, import time import random from multiprocessing import Process,Event def car(i,e): if not e.is_set(): print('car%s正在等待'%i) e.wait() #绿灯就直接过,红灯的话就阻塞等待, print('car%s通过路口'%i) def traffic_light(e): #专门负责改变红绿灯状态, print('\033[1;31m红灯亮\033[0m') # 事件在创立之初的状态是False,相当于我程序中的红灯 time.sleep(2) # 红灯亮2s while True: if not e.is_set(): # False print('\033[1;32m绿灯亮\033[0m') e.set() elif e.is_set(): print('\033[1;31m红灯亮\033[0m') e.clear() time.sleep(2) if __name__ == '__main__': e = Event() Process(target=traffic_light,args=[e,]).start() for i in range(50): time.sleep(random.randrange(0,5,2)) Process(target=car,args=[i,e]).start()

#import queue,这个队列,仅仅是数据结构,不能进行进程间通信 #from multiprocessing import Queue,这个队列能维护一个先进先出的秩序 ,也能进行IPC import time import random from multiprocessing import Process,Queue def producer(q): for i in range(10): time.sleep(random.random()) q.put('包子%s'%i) print('\033[1;31m生产了包子%s\033[0m'%i) def consumer(q): for i in range(5): food = q.get() #如果get不到,就会一直阻塞在这里,直到有put. print(food) print('\033[1;32m消费了%s\033[0m' %food) time.sleep(random.uniform(1,2)) if __name__ == '__main__': q = Queue() p1 = Process(target=producer,args=[q]) #生产包子进程 p2 = Process(target=consumer,args=[q]) #消费包子进程 p3 = Process(target=consumer,args=[q]) #消费包子进程 p1.start() p2.start() p3.start()
转载请注明原文地址: https://www.6miu.com/read-1950242.html

最新回复(0)