I use a python manager to share data between processes. I wanted to break up my code into smaller pieces and created a function 'soFactory' that would take a dictionary of name/object pairs and register the object with the manager under the given name. In the simple example below I create two lists and register them with the manager. If I use soFactory the manager returns only one of the lists (last one registered) regardless which name I reference. If I unravel the logic of soFactory I get access to the correct object by referencing their registered names. I must be missing something obvious but not seeing it.
# ------------ prototype for SO question -------
from sys import stderr
def soFactory(dictofSo, manager):
"""shared object factory"""
for n,t in dictofSo.items():
print >>stderr, 'created item',n,t
manager.register(n, callable=lambda: t)
def soRoutine(n, t, manager):
manager.register(n, callable=lambda: t)
def test_soFactory(useFactory=True):
"""tests the soFactory function"""
from multiprocessing import managers
m = managers.BaseManager(address='/var/tmp/tqps-test', authkey='abc123')
mySOlist = {'L1': [1],'L2':[2]}
if useFactory:
soFactory(mySOlist, m)
else:
for n, t in mySOlist.items():
soRoutine(n, t, m)
m.start()
m.L1().append('only in L1!')
print >>stderr, m.L1(), m.L2()
>>> test_soFactory()
created item L2 [2]
created item L1 [1]
[1, 'only in L1!'] [1, 'only in L1!']
>>> test_soFactory(useFactory=False)
[1, 'only in L1!'] [2]
>>>