gpt4 book ai didi

python - 来自远程管理器的 AutoProxy 对象中的字典不可订阅

转载 作者:行者123 更新时间:2023-12-03 12:58:56 25 4
gpt4 key购买 nike

这是我的代码。

from multiprocessing.managers import BaseManager
from threading import Thread

def manager1():
my_dict = {}
my_dict['key'] = "value"

print(my_dict['key']) #this works

class SyncManager(BaseManager): pass

SyncManager.register('get_my_dict', callable=lambda:my_dict)
n = SyncManager(address=('localhost', 50001), authkey=b'secret')
t = n.get_server()
t.serve_forever()

def get_my_dict_from_the_manager():
class SyncManager(BaseManager): pass

SyncManager.register('get_my_dict')
n = SyncManager(address=('localhost', 50001), authkey=b'secret')
n.connect()
my_dict = n.get_my_dict()
return my_dict

thread1 = Thread(target=manager1)
thread1.daemon = True
thread1.start()
my_dict = get_my_dict_from_the_manager()
print(my_dict.keys()) #this works
print(my_dict['key']) #DOES NOT WORK

在脚本的最后一行,我尝试通过键下标访问字典 my_dict 中的值。这会引发错误。这是我的终端输出:

value
['key']
Traceback (most recent call last):
File "/home/magnus/PycharmProjects/docker-falcon/app/so_test.py", line 31, in <module>
print(my_dict['key'])
TypeError: 'AutoProxy[get_my_dict]' object is not subscriptable

Process finished with exit code 1

看起来 AutoProxy 对象的行为有点像它应该代理的 dict,但不完全是。有没有办法让它成为可订阅的?

最佳答案

问题在于 AutoProxy 对象没有公开 dict 通常具有的 __getitem__ 方法。回答我的similar question允许您通过键访问项目:只需将 print(my_dict['key']) 替换为 print(my_dict.get('key'))

关于python - 来自远程管理器的 AutoProxy 对象中的字典不可订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com