1, a number of processes, parameters to the instance method, successive runs without problems, the code is as follows:
Import Multiprocessingclass MyClass (object): def calc (self,num): print ' The number is ', numif __name__ = = ' __ main__ ': mc = MyClass () ths = [multiprocessing. Process (target = Mc.calc,args = (i,)) for I in a range (3)] for th in ths: Th.start ()
Operation Result:
The number is 0the number was 1the number is 2
2, put in the process pool together concurrency
Import Multiprocessingclass MyClass (object): def calc (self,num): print ' The number is ', numif __name__ = = ' __ main__ ': mc = MyClass () pool = multiprocessing. Pool (processes = 3) Pool.apply_async (Mc.calc, (3,)) Pool.apply_async (Mc.calc, (4,)) Pool.apply_ Async (Mc.calc, (5,)) pool.close () pool.join () print ' end! '
Operation Result:
end!
Only the final result is printed, and the instance method passed in is not running at all!!!
3, the solution is as follows:
Import Multiprocessingimport copy_regimport typesdef _reduce_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, _reduce_method) class MyClass (object): def calc (self,num): print ' The number is ', numif __name__ = = ' __main__ ': mc = MyClass () pool = multiprocessing. Pool (processes = 3) Pool.apply_async (Mc.calc, (3,)) Pool.apply_async (Mc.calc, (4,)) Pool.apply_async ( Mc.calc, (5,)) pool.close () pool.join () print ' end! '
Operation Result:
The number is 4the number was 3the number is 5end!
When an instance method is used in the process pool of a child process, the program actually calls multiprocessing. Pipe to serialize data. python2.7, Multiprocessing.pipe is implemented using C language and is immediately called
Pickle_dumps, instead of calling Forkingpickler, the instance method passed in does not work.
However, if you use Copy_reg to register an instance method, it is unlikely that it will become possible.
After the python3.0 version, the instance method can be serialized normally, no need to use Copy_reg.
Detailed instructions can also be consulted:
http://stackoverflow.com/questions/27318290/ Why-can-i-pass-an-instance-method-to-multiprocessing-process-but-not-a-multipro
Passing instance method issues in a process pool