Passing instance method issues in a process pool

Source: Internet
Author: User
Tags instance method

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.