Python multiprocessing.Manager包含值列表的dict

时间:2017-06-21 00:20:02

标签: python gdb python-multiprocessing gdb-python

我在进程之间共享一个字典,每个进程都在dict中插入一个条目,其中字典中键的值可以是list或gdb.Value实例。 每个流程都在做这样的事情

mydict["key"] = [[2], gdb.Value(someaddress), 3, 4]

当执行上面的行时,我得到以下错误,看起来像是因为我正在插入gdb.Value,如果是object()实例

,这是有效的
Traceback (most recent call last):
  File "/test.py", line 631, in insert
    mydict["key"] = [[2], 1, 3, 4]
  File "<string>", line 2, in __setitem__
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 774, in _callmethod
    raise convert_to_error(kind, result)
RemoteError: 
---------------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 240, in serve_client
    request = recv()
TypeError: Value object creation takes only 1 argument

我也看到了另一个问题,循环显示dict给我以下错误

Traceback (most recent call last):
  File "/test.py", line 1214, in <module>
    for item in mydict:
  File "<string>", line 2, in __getitem__
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 774, in _callmethod
    raise convert_to_error(kind, result)

1 个答案:

答案 0 :(得分:0)

请发布失败的自包含示例。这是我最好的尝试,从你写的东西中猜到,但它运作良好:

import multiprocessing as mp

def e(tag, d):
    d[tag] = [[tag], tag, tag, tag]

if __name__ == '__main__':
    d = mp.Manager().dict()
    ps = []
    for i in range(4):
        ps.append(mp.Process(target=e, args=(str(i), d)))
        ps[-1].start()
    for p in ps:
        p.join()
    print(d)

它打印出来:

{'0': [['0'], '0', '0', '0'],
 '1': [['1'], '1', '1', '1'],
 '2': [['2'], '2', '2', '2'],
 '3': [['3'], '3', '3', '3']}