栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python:从该类内部对作为类的数据成员的函数进行多处理的有效解决方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python:从该类内部对作为类的数据成员的函数进行多处理的有效解决方法

史蒂文·贝萨德(Steven Bethard
)发布了一种允许对方法进行腌制/解腌的方法。您可以这样使用它:

import multiprocessing as mpimport copy_regimport typesdef _pickle_method(method):    # Author: Steven Bethard    # http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods    func_name = method.im_func.__name__    obj = method.im_self    cls = method.im_class    cls_name = ''    if func_name.startswith('__') and not func_name.endswith('__'):        cls_name = cls.__name__.lstrip('_')    if cls_name:        func_name = '_' + cls_name + func_name    return _unpickle_method, (func_name, obj, cls)def _unpickle_method(func_name, obj, cls):    # Author: Steven Bethard    # http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods    for cls in cls.mro():        try: func = cls.__dict__[func_name]        except KeyError: pass        else: break    return func.__get__(obj, cls)# This call to copy_reg.pickle allows you to pass methods as the first arg to# mp.Pool methods. If you comment out this line, `pool.map(self.foo, ...)` results in# PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup# __builtin__.instancemethod failedcopy_reg.pickle(types.MethodType, _pickle_method, _unpickle_method)class MyClass(object):    def __init__(self):        self.my_args = [1,2,3,4]        self.output  = {}    def my_single_function(self, arg):        return arg**2    def my_parallelized_function(self):        # Use map or map_async to map my_single_function onto the        # list of self.my_args, and append the return values into        # self.output, using each arg in my_args as the key.        # The result should make self.output become        # {1:1, 2:4, 3:9, 4:16}        self.output = dict(zip(self.my_args,         pool.map(self.my_single_function, self.my_args)))

然后

pool = mp.Pool()   foo = MyClass()foo.my_parallelized_function()

产量

print foo.output# {1: 1, 2: 4, 3: 9, 4: 16}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640102.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号