SyncManager.register()
类方法的第一个参数必须是字符串,而不是 cass:
SyncManager.register('set', set)
但您还需要为集合注册代理:
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)