multiprocessing.Manager nested shared objects doesn#39;t work with Queue(multiprocessing.Manager 嵌套共享对象不适用于队列)
问题描述
Python docs of the multiprocessing
module state:
Changed in version 3.6: Shared objects are capable of being nested. For example, a shared container object such as a shared list can contain other shared objects which will all be managed and synchronized by the
SyncManager
.
This does work with list
and dict
. However, if I try to create a shared Queue
inside a shared dict
, I get an error:
>>> from multiprocessing import Manager
>>> m = Manager()
>>> d = m.dict()
>>> d['a'] = m.list()
>>> d['b'] = m.dict()
>>> d['c'] = m.Queue()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 2, in __setitem__
File "/usr/lib/python3.6/multiprocessing/managers.py", line 772, in _callmethod
raise convert_to_error(kind, result)
multiprocessing.managers.RemoteError:
---------------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/managers.py", line 228, in serve_client
request = recv()
File "/usr/lib/python3.6/multiprocessing/connection.py", line 251, in recv
return _ForkingPickler.loads(buf.getbuffer())
File "/usr/lib/python3.6/multiprocessing/managers.py", line 881, in RebuildProxy
return func(token, serializer, incref=incref, **kwds)
TypeError: AutoProxy() got an unexpected keyword argument 'manager_owned'
---------------------------------------------------------------------------
Seems like https://hg.python.org/cpython/rev/39e7307f9aee is the changeset which introduced nested shared objects.
The error is caused by AutoProxy
currently not handling all BaseProxy
arguments. There's a pull request which has not been merged yet. You either need to monkey-patch AutoProxy
, or you look into multiprocessing.managers.py
and apply the changes in the patch here directly to your source code.
It's really important to fix both lines in the patch to prevent a memory leak in the server process. The manager_owned
-flag is used to let the BaseProxy
code know, when to skip a reference increment for a proxy the manager owns himself (through nesting).
这篇关于multiprocessing.Manager 嵌套共享对象不适用于队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:multiprocessing.Manager 嵌套共享对象不适用于队列
基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01