我想做的是在子类之间共享一个字典Process
,当一个进程更新字典时,另一个进程会被通知使用它。这在下面的代码中进行了说明,其中MyProducer
开始填充字典并在每次迭代中触发一个事件来通知MyConsumer
处理字典。除了字典MyConsumer
为空的部分之外,一切都有效......
from multiprocessing import Process, Manager, Event
class MyProducer(Process):
increment = 0
def __init__(self, dictionary, event):
Process.__init__(self)
self.dictionary = dictionary
self.event = event
def run(self):
while self.increment < 20:
self.dictionary[self.increment]=self.increment+10
self.increment = self.increment + 1
print("From producer: ", self.dictionary)
self.event.set()
while self.event.is_set() is True:
increment = self.increment
increment = increment + 1
class MyConsumer(Process):
def __init__(self, dictionary, event):
Process.__init__(self)
self.dictionary = dictionary
self.event = event
def run(self):
while True:
self.event.wait()
print("From consumer: ", self.dictionary)
self.event.clear()
if __name__ == "__main__":
with Manager() as manager:
state_dict = manager.dict()
state_ready = Event()
producerprocess = MyProducer(state_dict, state_ready)
consumerprocess = MyConsumer(state_dict, state_ready)
producerprocess.start()
consumerprocess.start()
输出是
Process MyProducer-2:
Traceback (most recent call last):
File "/usr/lib/python3.8/multiprocessing/managers.py", line 827, in _callmethod
conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "main.py", line 13, in run
self.dictionary[self.increment]=self.increment+10
File "<string>", line 2, in __setitem__
File "/usr/lib/python3.8/multiprocessing/managers.py", line 831, in _callmethod
self._connect()
File "/usr/lib/python3.8/multiprocessing/managers.py", line 818, in _connect
conn = self._Client(self._token.address, authkey=self._authkey)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 502, in Client
c = SocketClient(address)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 630, in SocketClient
s.connect(address)
FileNotFoundError: [Errno 2] No such file or directory
更新
我的目的是了解为什么字典不适用于 Process 子类。我知道您可以在互联网上找到的所有有效案例。实际上我有一个很好的解决方案,只需将 dict 替换为 queue,我想了解为什么 dict 不起作用。
from multiprocessing import Process, Queue, Event
class MyProducer(Process):
increment = 0
def __init__(self, queue, event):
Process.__init__(self)
self.queue = queue
self.event = event
def run(self):
while self.increment < 20:
self.queue.put([self.increment,self.increment+10])
self.increment = self.increment + 1
print("From producer: ", self.queue.qsize())
self.event.set()
while self.event.is_set() is True:
increment = self.increment
increment = increment + 1
class MyConsumer(Process):
def __init__(self, queue, event):
Process.__init__(self)
self.queue = queue
self.event = event
def run(self):
while True:
self.event.wait()
print("From consumer: ", self.queue.qsize())
self.event.clear()
if __name__ == "__main__":
state_queue = Queue()
state_ready = Event()
producerprocess = MyProducer(state_queue, state_ready)
consumerprocess = MyConsumer(state_queue, state_ready)
producerprocess.start()
consumerprocess.start()