Python multi-process using function encapsulation

Source: Internet
Author: User

Tag:sys   append   python   children    execution     super    field    elf    method    

import multiprocessing as mpfrom multiprocessing import processclass  Myprocess (process):     "" "     custom multi-process, inherited from native process, to get multi-process results to queue      "" "    def __init__ (SELF, FUNC, ARGS, Q):         super (myprocess, self). __init__ ()          self.func = func        self.args  = args        self.res =  '          self.q = q         #self. _ daemonic = true         #self. _daemonic = true     def run (self):         self.res =  self.func (*self.args)    &Nbsp;    self.q.put ((self.func.__name__, self.res))     def  Use_multiprocessing (func_list):     #os. System (' export pythonoptimize=1 ')   #   Solutions  daemonic processes are not allowed to have children  Issues     q = mp. Queue ()   #  queues, storing multi-process results here, inter-process sharing,  multi-process must use   multiprocessing  queue     proc_list = []    res = []    for  func in func_list:        proc = myprocess ( func[' func '], args=func[' args '], q=q)         proc.start ()         proc_list.append (proc)     for p  In proc_list:        p.join ()      while not q.empty ():         r = q.get ()         res.append (R)     return res       when used, the parameters of functions and functions that require multi-process execution are treated as fields, and a list  is passed to the use_multiprocessing  method.


Python multi-process using function encapsulation

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.