これは私の現在のコードですが、主な問題はSemphoreを使用して2つのプロセスの出力を制御することですが、Semphoreがglobalyを変更しない、 Semporeを2に変更します。「消費者」はSemporeがゼロであると考えています。これは、それが永遠に待つ原因となります。セマフォの値がグローバルに変更されない理由
from multiprocessing import Process, Semaphore, Queue
import time
from random import random
buffer = Queue(10)
empty = Semaphore(2)
full = Semaphore(0)
class Consumer(Process):
def run(self):
global buffer, empty, full
while True:
time.sleep(4)
print(full)
full.acquire()
buffer.get()
print('Consumer get')
time.sleep(1)
empty.release()
class Producer(Process):
def run(self):
global buffer, empty, full
while True:
empty.acquire()
print ('Producer put ')
time.sleep(1)
full.release()
buffer.put(1)
print(full)
if __name__ == '__main__':
p = Producer()
c = Consumer()
p.daemon = c.daemon = True
p.start()
c.start()
p.join()
c.join()
print ('Ended!')
、出力は私が「消費者」のプロセスが変更を検出させるために何をすべきかを知りません
Producer put
<Semaphore(value=1)>
Producer put
<Semaphore(value=2)>
<Semaphore(value=0)>
です。
これをProcessesに渡します: 'Process(args =(empty、)) '。ワーカー関数を使用しないので、 'self._args'を使ってargsにアクセスする必要があります。 – stovfl