Created
January 4, 2012 19:59
-
-
Save astrofrog/1561756 to your computer and use it in GitHub Desktop.
How to use map with methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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