问题描述
我想做的是在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,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用队列替换,我想了解为什么dict无法正常工作。
from multiprocessing import Process,Queue,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,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()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)