我需要一个可靠的代码,并且我已经做了很多相关的测试,似乎它只能序列化字符串而不是序列化工作表对象
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()
答案 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())