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

时间:2017-12-27 06:24:06

标签: python

我需要一个可靠的代码,并且我已经做了很多相关的测试,似乎它只能序列化字符串而不是序列化工作表对象

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()

1 个答案:

答案 0 :(得分:0)

您可以在类Runner中定义调用()方法,如果类定义调用方法,则可以将其实例作为函数调用。

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())