Python 将对象插入多处理管理器,dict

Python 将对象插入多处理管理器,dict,python,multiprocessing,shared-memory,Python,Multiprocessing,Shared Memory,我在test.py中有以下代码: manager = multiprocessing.Manager() cache = manager.dict() class Test: def __init__(self): pass 如果我试图将对象插入另一个文件的缓存中,例如 from test import * cache[1] = 1 #this works cache[2] = Test() #this fails/hangs 为什么第二个案例失败/挂起?是否可以将

我在
test.py
中有以下代码:

manager = multiprocessing.Manager()
cache = manager.dict()

class Test:
    def __init__(self):
        pass
如果我试图将对象插入另一个文件的缓存中,例如

from test import *
cache[1] = 1 #this works
cache[2] = Test() #this fails/hangs
为什么第二个案例失败/挂起?是否可以将对象插入manager.dict()中

编辑:在Linux上挂起,但在Windows上工作。Python3.7.3

使用普通dict更新manager.dict()

tests = {}
test = test()
tests[test.name] = test
# insert other tests in the normal dictionary

obj = multiprocessing.Manager()
obj_tests = obj.dict()
obj_tests.update(tests)
用普通dict更新manager.dict()

tests = {}
test = test()
tests[test.name] = test
# insert other tests in the normal dictionary

obj = multiprocessing.Manager()
obj_tests = obj.dict()
obj_tests.update(tests)

我认为
管理器
需要首先
启动
()(或通过
使用
“输入”),即元素设置代码正在等待相关服务器进程的响应,但从未收到响应,因此挂起。无论如何,在Linux下,我远离Windows,所以我不想对此发表评论

您发布的代码的简单修复方法是:

from test import manager, cache, Test
with manager:
    cache[1] = 1
    cache[2] = Test()
(通常不鼓励使用
import*


但是我想你可能想做得更多,所以修复会更复杂。

我认为
管理器
需要首先启动
启动
()(或者通过
使用
“输入”),也就是说,元素设置代码正在等待来自相关服务器进程的响应,但从未得到响应,因此挂起。无论如何,在Linux下,我远离Windows,所以我不想对此发表评论

您发布的代码的简单修复方法是:

from test import manager, cache, Test
with manager:
    cache[1] = 1
    cache[2] = Test()
(通常不鼓励使用
import*


但是我想你可能想做更多的事情,所以修复会更复杂。

nope仍然失败-你需要在python 3.7oops的unix环境上运行代码,我想我测试了我发布的代码。。。很明显,我有一个打字错误,因为它现在对我来说也失败了,我会尝试更长的时间!Py 2.7中的bc很奇怪代码在Unix上运行,但在3.7中挂起,不确定这是否有帮助我认为这可能与缓存和测试定义在同一个文件中有关猜测,
pickle
ing对象(或相反)会导致创建新的
Manager
,从而导致think失败。或者可能是由于fork导致Python自行运行,例如nope仍然失败-您需要在Python 3.7oops中的unix环境上运行代码,虽然我测试了发布的代码。。。很明显,我有一个打字错误,因为它现在对我来说也失败了,我会尝试更长的时间!Py 2.7中的bc很奇怪代码在Unix上运行,但在3.7中挂起,不确定这是否有帮助我认为这可能与缓存和测试定义在同一个文件中有关猜测,
pickle
ing对象(或相反)会导致创建新的
Manager
,从而导致think失败。或者可能是由于fork导致Python遍历自身,例如,我认为这样做可行,但我不希望在这种情况下使用嵌套dict我认为这样做可行,但在这种情况下我不希望使用嵌套dict