[英]Using sets with the multiprocessing module
I can't seem to share a set across processes using a Manager instance. 我似乎无法使用Manager实例跨进程共享集合。 A condensed version of my code:
我的代码的精简版:
from multiprocessing.managers import SyncManager
manager = SyncManager()
manager.start()
manager.register(Set)
I've also tried register(type(Set))
and register(Set())
, but I'm not overly surprised that neither of them worked (the first should evaluate to Class, I think). 我也尝试过
register(type(Set))
和register(Set())
,但我都不为它们都不起作用而感到惊讶(我认为第一个应该评估为Class)。
The exception I get in all cases is TypeError: __name__ must be set to a string object
in line 675 of managers.py. 我在所有情况下都遇到的异常是
TypeError: __name__ must be set to a string object
第675行TypeError: __name__ must be set to a string object
。
Is there a way of doing this, or do I need to investigate alternatives? 有没有办法做到这一点,或者我需要研究替代方法吗?
The first argument to the SyncManager.register()
class method must be a string , not a cass: SyncManager.register()
类方法的第一个参数必须是字符串 ,而不是cass:
SyncManager.register('set', set)
but you'll need to register a proxy for sets as well: 但您还需要注册一个代理集:
from multiprocessing.managers import MakeProxyType
BaseSetProxy = MakeProxyType('BaseSetProxy', (
'__and__', '__contains__', '__iand__', '__ior__',
'__isub__', '__ixor__', '__len__', '__or__', '__rand__', '__ror__', '__rsub__',
'__rxor__', '__sub__', '__xor__', 'add', 'clear', 'copy', 'difference',
'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint',
'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference',
'symmetric_difference_update', 'union', 'update']
))
class SetProxy(BaseSetProxy):
# in-place hooks need to return `self`, specify these manually
def __iand__(self, value):
self._callmethod('__iand__', (value,))
return self
def __ior__(self, value):
self._callmethod('__ior__', (value,))
return self
def __isub__(self, value):
self._callmethod('__isub__', (value,))
return self
def __ixor__(self, value):
self._callmethod('__ixor__', (value,))
return self
SyncManager.register('set', set, SetProxy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.