Хочу через Pool вызывать метод объекта. В методе внутренней переменной пристаивается некое значение. Если делать последовательно - все работает. В потоках значение не сохраняется.
Небольшой пример:
#!/usr/bin/python
from multiprocessing import Pool
#------------------------------------------------------------
# Some hack to make methods be pickable
def _pickle_method(method):
func_name = method.im_func.__name__
obj = method.im_self
cls = method.im_class
return _unpickle_method, (func_name, obj, cls)
def _unpickle_method(func_name, obj, cls):
for cls in cls.mro():
try:
func = cls.__dict__[func_name]
except KeyError:
pass
else:
break
return func.__get__(obj, cls)
import copy_reg
import types
copy_reg.pickle(types.MethodType, _pickle_method, _unpickle_method)
#------------------------------------------------------------
class MyClass(object):
''' Simple simple class '''
def __init__(self):
''' got just single variable '''
self.x = 0
def setX(self, value=0):
''' and just a single method. Returns instance variable '''
self.x = value
return self.x
if __name__ == "__main__":
mylist = []
# first - create several objects of MyClass
for i in range(10):
mylist.append(MyClass())
# set internal variable via instance method
mylist[i].setX(i ** 2)
# check - expecting squares from zero to nine
for c in mylist:
print c.x, '\t',
print ''
print '*'*80
# Now we do this in parralel
pool = Pool(5)
results = [pool.apply_async(MyClass.setX, (mylist[i], i ** 3)) for i in range(len(mylist))]
# results via get methos are ok
for r in results:
print r.get(), ' - ',
print ''
print '*'*80
# but class instanses did't changed :(
for c in mylist:
print c.x, '\t',
'''
RESULTS:
0 1 4 9 16 25 36 49 64 81
********************************************************************************
0 - 1 - 8 - 27 - 64 - 125 - 216 - 343 - 512 - 729 -
********************************************************************************
0 1 4 9 16 25 36 49 64 81
'''
Спасибо.