输出两行便卡住了,但是在原书的输出是完整的多行内容,多次确认过代码后发现代码无误但是输出内容与原文不同。望大佬解答十分感谢

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')

输出两行便卡住了,但是在原书的输出是完整的多行内容,多次确认过代码后发现代码无误但是输出内容与原文不同。望大佬解答十分感谢

以上是 输出两行便卡住了,但是在原书的输出是完整的多行内容,多次确认过代码后发现代码无误但是输出内容与原文不同。望大佬解答十分感谢 的全部内容, 来源链接: utcz.com/a/157046.html

回到顶部