如何在 python 中使用 multiprocessing.Manager() 数组
How to work with a multiprocessing.Manager() Array in python
我正在使用 Python 的多处理启动工作进程。我需要能够更新子进程中可以在父进程中看到的数组。我使用 multiprocessing.Manager()
来完成。
a = multiprocessing.Manager().Array('f', [0,0])
a
<ArrayProxy object, typeid 'Array' at 0x7f4e2b4eeda0>
a.value
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
AttributeError: 'ArrayProxy' object has no attribute 'value'
当我创建一个Value('f', 0.0)
时,我可以这样操作它。当我查看 dir(a)
时,我没有看到任何明显的函数或属性可以调用,python 中的文档对这一点非常模糊。
如果我理解正确,你需要自动更新 Array
中的多个值。
这种情况,我建议你:
a.acquire() # Using its internal lock object
a[0] = ... # Do what ever you want
a[1] = ...
a.release()
a
是一个 proxy 对象
<ArrayProxy object, typeid 'Array' at 0x7f4e2b4eeda0>
A proxy is an object which refers to a shared object which lives (presumably) in a different process. The shared object is said to be the referent of the proxy.
代理对象与真实对象的关系如下图所示,在multiprocessing/managers中。 py
SyncManager.register('Value', Value, ValueProxy)
SyncManager.register('Array', Array, ArrayProxy)
我们看到 ArrayProxy
支持的运算符
ArrayProxy = MakeProxyType('ArrayProxy', (
'__len__', '__getitem__', '__setitem__', '__getslice__', '__setslice__'
)) # XXX __getslice__ and __setslice__ unneeded in Py3.0
数组其实就是array.array(...)
Value(typecode, value) 不同于 数组
Create an object with a writable value
attribute and return a proxy for it.
class ValueProxy(BaseProxy):
_exposed_ = ('get', 'set')
def get(self):
return self._callmethod('get')
def set(self, value):
return self._callmethod('set', (value,))
value = property(get, set)
我正在使用 Python 的多处理启动工作进程。我需要能够更新子进程中可以在父进程中看到的数组。我使用 multiprocessing.Manager()
来完成。
a = multiprocessing.Manager().Array('f', [0,0])
a
<ArrayProxy object, typeid 'Array' at 0x7f4e2b4eeda0>
a.value
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
AttributeError: 'ArrayProxy' object has no attribute 'value'
当我创建一个Value('f', 0.0)
时,我可以这样操作它。当我查看 dir(a)
时,我没有看到任何明显的函数或属性可以调用,python 中的文档对这一点非常模糊。
如果我理解正确,你需要自动更新 Array
中的多个值。
这种情况,我建议你:
a.acquire() # Using its internal lock object
a[0] = ... # Do what ever you want
a[1] = ...
a.release()
a
是一个 proxy 对象
<ArrayProxy object, typeid 'Array' at 0x7f4e2b4eeda0>
A proxy is an object which refers to a shared object which lives (presumably) in a different process. The shared object is said to be the referent of the proxy.
代理对象与真实对象的关系如下图所示,在multiprocessing/managers中。 py
SyncManager.register('Value', Value, ValueProxy)
SyncManager.register('Array', Array, ArrayProxy)
我们看到 ArrayProxy
支持的运算符ArrayProxy = MakeProxyType('ArrayProxy', (
'__len__', '__getitem__', '__setitem__', '__getslice__', '__setslice__'
)) # XXX __getslice__ and __setslice__ unneeded in Py3.0
数组其实就是array.array(...)
Value(typecode, value) 不同于 数组
Create an object with a writable
value
attribute and return a proxy for it.
class ValueProxy(BaseProxy):
_exposed_ = ('get', 'set')
def get(self):
return self._callmethod('get')
def set(self, value):
return self._callmethod('set', (value,))
value = property(get, set)