Python: Efficient workaround for multiprocessing a function that is a data member of a class, from within that class

前端 未结 3 1182
春和景丽
春和景丽 2020-12-09 06:36

I\'m aware of various discussions of limitations of the multiprocessing module when dealing with functions that are data members of a class (due to Pickling problems).

3条回答
  •  隐瞒了意图╮
    2020-12-09 07:03

    There is a better elegant solution i believe. Add the following line to a code that does multiprocessing with the class and you can still pass the method through the pool. the codes should go above the class

    import copy_reg
        import types
    
        def _reduce_method(meth):
            return (getattr,(meth.__self__,meth.__func__.__name__))
        copy_reg.pickle(types.MethodType,_reduce_method)
    

    for more understanding of how to pickle a method please see below http://docs.python.org/2/library/copy_reg.html

提交回复
热议问题