Last active
November 17, 2015 11:55
-
-
Save daTokenizer/6e631bb1d1b6afb3e8d6 to your computer and use it in GitHub Desktop.
a skeleton for a functional usage of the python multiprocessing map functionality
This file contains 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
from multiprocessing import Pool | |
DEFAULT_VALUE = thing | |
class MapRunner(object): | |
def __init__(self): | |
p = pool(i,init_globals) | |
def init_globals(): | |
global g1 | |
global g2 | |
g1 = somthing | |
g2 = whatever | |
def run(): | |
p.map(f,X_collection) | |
def f(x): | |
try: | |
a = w(x,g1) | |
b = y(x+a) | |
return z(x,g2) | |
except: | |
return DEFAULT_VALUE | |
class Mappable(object): | |
def w(x,g): | |
pass | |
def y(x): | |
pass | |
def z(w,g) | |
pass | |
# Note these two functions should basically be copied as is.. | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment