pythoncPickle.pickling错误:无法pickle<type'instancemethod>。我知道原因,但不知道如何解决我

2024-10-02 02:29:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: instanceimportselfindex序列化defresultsheet
1条回答
网友
1楼 · 发布于 2024-10-02 02:29:45

可以在类运行器中定义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()

-

结果:

^{pr2}$

或者您可以使用copy\u 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())

相关问题 更多 >

    热门问题