Skip to content

Instantly share code, notes, and snippets.

@astrofrog
Created January 4, 2012 19:59
Show Gist options
  • Select an option

  • Save astrofrog/1561756 to your computer and use it in GitHub Desktop.

Select an option

Save astrofrog/1561756 to your computer and use it in GitHub Desktop.
How to use map with methods
# The following is Steven Bethard's functions to pickle methods - required to
# use multiprocessing.Pool with model.run
import copy_reg
import types
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)
copy_reg.pickle(types.MethodType, _pickle_method, _unpickle_method)
# Now define class and run Pool.map() on MyClass.do()
class MyClass(object):
def __init__(self):
pass
def do(self, a):
return a * a
from multiprocessing import Pool
p = Pool()
m = MyClass()
results = p.map(m.do, range(10))
print results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment