I am using multiprocessing lib for accelerating process of class creation. Minimal example of my code:
from matplotlib.backends.backend_qt5agg import FigureCanvas
class Custom_Class(FigureCanvas)
.
.
.
def generate_class_func(list_of_dfs, arg1, arg2):
list_of_custom_classes = list()
for df in list_of_dfs:
custom_class = Custom_Class(df, arg1, arg2)
list_of_custom_classes.append(custom_class)
return list_of_custom_classes
def main():
import multiprocessing as mp
with mp.Pool() as p:
list_of_classes_list = p.starmap(generate_class_func, zip(list_of_dfs, repeat(arg1), repeat(arg2)))
p.close()
p.join()
if __name__ == '__main__':
main()
However, I got multiprocess.pool.MaybeEncodingError: Error sending result: ... Reason: 'TypeError("cannot pickle 'Custom_Class' object")' error. I also tried pathos.ProcessingPool module but the error is same. How can I return custom objects with multiprocessing?
I'm the author of
dillandmultiprocess. One easy trick to make thing more serializable is to usemultiprocessinstead ofmultiprocessing. The former is a fork of the latter that usesdillinstead ofpickle, so you immediately get the ability to serialize more objects, including most custom classes.