输出两行便卡住了,但是在原书的输出是完整的多行内容,多次确认过代码后发现代码无误但是输出内容与原文不同。望大佬解答十分感谢
import time
from multiprocessing import Lock, Process, Queue, Semaphore
from random import random
buffer = Queue(10)
empty = Semaphore(2)
full = Semaphore(0)
lock = Lock()
class Consumer(Process):
def run(self):
global buffer, empty, full, lock
while True:
full.acquire()
lock.acquire()
print(f'Consumer get {buffer.get()}')
time.sleep(1)
lock.release()
empty.release()
class Producer(Process):
def run(self):
global buffer, empty, full, lock
while True:
empty.acquire()
lock.acquire()
num = random()
print(f'Producer put {num}')
buffer.put(num)
time.sleep(1)
lock.release()
full.release()
if name == '__main__':
p = Producer()
c = Consumer()
p.daemon = True
c.daemon = True
p.start()
c.start()
p.join()
c.join()
print('Main Process Ended')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论