0

我将 multiprocessing.Manaager().Queue() 的实例对象 (q) 分配给 multiprocessing.Manager().dict() 的实例对象 (d) 的一项,但我无法重用 dict 中的 q .

Python 的版本是 2.7.15。

#!/usr/bin/env python
# _*_ coding:utf-8 _*_


from multiprocessing import Process, Manager

def f(d):
    xx = d['10.10.10.102_1024']
    xx.put(11)
    d['ddd'] = 8888888888
def test1(d,manager):
    q = manager.Queue()
    try:
        d['10.10.10.102_1024'] = q
    except Exception as error:
        print error

if __name__ == '__main__':
    manager = Manager()

    d = manager.dict()

    test1(d,manager)
    p = Process(target=f, args=(d,))
    p.start()
    p.join()
    print(d)    

输出

Process Process-2:
Traceback (most recent call last):
  File "/Users/shicongming/anaconda2/lib/python2.7/multiprocessing/process.py", line 267, in _bootstrap
    self.run()
  File "/Users/shicongming/anaconda2/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "test_dict.py", line 8, in f
    xx = d['10.10.10.102_1024']
  File "<string>", line 2, in __getitem__
  File "/Users/shicongming/anaconda2/lib/python2.7/multiprocessing/managers.py", line 774, in _callmethod
    raise convert_to_error(kind, result)
RemoteError: 
---------------------------------------------------------------------------
Unserializable message: Traceback (most recent call last):
  File "/Users/shicongming/anaconda2/lib/python2.7/multiprocessing/managers.py", line 288, in serve_client
    send(msg)
TypeError: can't pickle thread.lock objects

---------------------------------------------------------------------------
{'10.10.10.102_1024': <Queue.Queue instance at 0x10f5b01b8>}
4

0 回答 0