python cPickle.PicklingError:无法腌制<type'instancemethod'>。我知道原因,但我不知道如何解决它

循环播放

我需要一个可靠的代码,并且已经完成了许多相关的测试,看来它只能序列化字符串,而不能序列化工作表对象

from multiprocessing import Pool
import openpyxl


def proxy(cls_instance, index):
    return cls_instance.func(index)


class Runner(object):
    def __init__(self, obtest, sheet):
        self.obtest = obtest
        self.sheet = sheet

    def func(self, index):
        return index


class OBTest(object):

    def run(self):
        wb = openpyxl.load_workbook('/Users/attackt/Downloads/excelfile.xlsx')
        pool = Pool(processes=5)
        sheets = wb.worksheets
        # sheets = ['A', 'B', 'C', 'D']
        result = []
        for index, sheet in enumerate(sheets):
            instance = Runner(self, sheet)
            result.append(pool.apply_async(proxy, (instance, index)))
        pool.close()
        pool.join()

        for data in result:
            print data.get()

if __name__ == '__main__':
    OBTest().run()
宾比斯

您可以在类Runner内定义一个call()方法,如果该类定义了call方法,则可以将其实例作为函数调用。

from multiprocessing import Pool
import openpyxl

def proxy(cls_instance, index):
    return cls_instance.func(index)


class Runner(object):
    def __init__(self, obtest, sheet):
        self.obtest = obtest
        self.sheet = sheet

    def func(self, index):
        return index

    def __call__(self, x):
        return self.func(x)


class OBTest(object):

    def run(self):
        #wb = openpyxl.load_workbook('/Users/attackt/Downloads/excelfile.xlsx')
        pool = Pool(processes=5)
        #sheets = wb.worksheets
        sheets = ['A', 'B', 'C', 'D']
        result = []
        for index, sheet in enumerate(sheets):
            instance = Runner(self, sheet)
            result.append(pool.apply_async(proxy, (instance, index)))
        pool.close()
        pool.join()

        for data in result:
            print data.get()

if __name__ == '__main__':
    OBTest().run()

--

结果:

0
1
2
3

或者您可以使用copy_reg

from multiprocessing import Pool
import types
import copy_reg
import openpyxl


def _pickle_method(m):
    if m.im_self is None:
        return getattr, (m.im_class, m.im_func.func_name)
    else:
        return getattr, (m.im_self, m.im_func.func_name)


copy_reg.pickle(types.MethodType, _pickle_method)


def proxy(cls_instance, index):
    return cls_instance.func(index)


class Runner(object):
    def __init__(self, obtest, sheet):
        self.obtest = obtest
        self.sheet = sheet

    def func(self, index):
        return index


class OBTest(object):

    def run(self):
        #wb = openpyxl.load_workbook('/Users/attackt/Downloads/excelfile.xlsx')
        pool = Pool(processes=5)
        #sheets = wb.worksheets
        sheets = ['A', 'B', 'C', 'D']
        result = []
        for index, sheet in enumerate(sheets):
            instance = Runner(self, sheet)
            result.append(pool.apply_async(proxy, (instance, index)))
        pool.close()
        pool.join()

        for data in result:
            print data.get()

if __name__ == '__main__':
    OBTest().run()

-

im_self is the class instance object;
im_func is the function object;
im_class is the class of im_self for bound methods or the class that asked for the method for unbound methods;

-

可以腌制和不腌制的东西

None, True, and False
integers, long integers, floating point numbers, complex numbers
normal and Unicode strings
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module
built-in functions defined at the top level of a module
classes that are defined at the top level of a module
instances of such classes whose __dict__ or the result of calling 
__getstate__() is picklable (see section The pickle protocol for details).

-

如果我的解决方案对您有帮助,请投票给我答案。因为我需要解锁我的帐户!谢谢!

请参见以下方法!

def run(self):
    wb = openpyxl.load_workbook('/Users/justTest/Downloads/outputlistmobile.xlsx')
    pool = Pool(processes=5)

    # sheets = wb.worksheets
    # # sheets = ['A', 'B', 'C', 'D']
    result = []
    sheets = wb.get_sheet_names()
    for index, sheet in enumerate(sheets):
        instance = Runner(self, sheet)
        result.append(pool.apply_async(proxy, (instance, index)))
    pool.close()
    pool.join()

    for data in result:
        print(data.get())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章