问题描述
使用多处理模块,我编写了一个服务器来服务dict
.现在,尝试通过客户端通过键访问该字典,我得到以下错误(server.py
和client.py
在文章的底部):
Using the multiprocessing module, I wrote a server to serve a dict
. Now, trying to access that dict by key with a client, I get the following error (server.py
and client.py
are at the bottom of the post):
Traceback (most recent call last):
File "client.py", line 19, in <module>
item = my_dict[key]
TypeError: 'AutoProxy[get_dict]' object is not subscriptable
我相信这是由于我register()
与我的SyncManager
一起编辑的dict
被腌制并作为AutoProxy
对象传递的事实.当我用print(dir(my_dict))
检查AutoProxy对象的方法时,这就是我得到的:
I believe this is due to the fact that the dict
that I register()
-ed with my SyncManager
gets pickled and passed on as an AutoProxy
object. When I check the methods of the AutoProxy object with print(dir(my_dict))
, this is what I get:
['_Client', '__builtins__', '__class__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_address_to_local', '_after_fork', '_authkey', '_callmethod', '_close', '_connect', '_decref', '_exposed_', '_getvalue', '_id', '_idset', '_incref', '_isauto', '_manager', '_mutex', '_serializer', '_tls', '_token', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
与server.py
中print(dir(my_dict))
的输出不同:
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
看起来这个Autoproxy对象保留了dict
对象的某些方法,但显然不是全部.至关重要的是,它没有保留__getitem__
方法,这阻止了我通过键访问项目.
It looks like this Autoproxy object retains some methods of the dict
object, but apparently not all of them. Crucially it doesn't retain the __getitem__
method, which prevents me from accessing items by key.
如何通过键访问dict
项目?同样,关于Proxy
如何与Python多处理结合使用的任何解释也将非常有帮助.
How can I access the dict
items by key? Also any explanation of how Proxy
s work with Python multiprocessing would be very helpful.
注意:我不需要修改dict
值,只需要通过键提取它们即可.
Note: I don't need to modify the dict
values, I just need to extract them by key.
server.py:
server.py:
from multiprocessing.managers import SyncManager
my_dict = {'item_1': 1, 'item_2':2}
def get_my_dict():
return my_dict
class MyManager(SyncManager):
pass
if __name__ == "__main__":
port_num = 4343
MyManager.register("get_dict", get_my_dict)
manager = MyManager(("127.0.0.1", port_num), authkey=b"password")
manager.start()
input("Press any key to kill server".center(50, "-"))
manager.shutdown
client.py
client.py
from multiprocessing.managers import SyncManager
import sys
class MyManager(SyncManager):
pass
MyManager.register("get_dict")
if __name__ == "__main__":
port_num = 4343
manager = MyManager(("127.0.0.1", port_num), authkey=b"password")
manager.connect()
my_dict = manager.get_dict()
print("dict = %s" % (dir(my_dict)))
keys = list(my_dict.keys())
print(keys)
for key in keys:
print(my_dict[key])
推荐答案
您可以使用get
来获得任一版本的字典元素.
You can use get
to, well, get an element of a dictionary, in either version.
这篇关于通过多处理中的键访问字典中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!