Python Multi-process multiprocessing

Source: Internet
Author: User
Import multiprocessingimport time# Specific processing function, responsible for handling a single task Def func (msg):  # for I in range (3):    print (msg)    Time.sleep (1)    return "done" + msgif __name__ = = "__main__":    # Process pool, create multiple processes, execute    pool = multiprocessing in parallel. Pool (processes=4)    # Add the results of the run to a list, focusing on the execution results of each process result    = []    # Production MSG, and join the process pool for    I in range:        msg = "Hello%d"% (i)        # Apply_async It is non-blocking and supports results returned with callback        Result.append (Pool.apply_async (func, (msg,))        # Close the process pool so that it does not accept the new task        pool.close ()        # The main process is blocked waiting for the child process to exit, and the Join method must be used after close or terminate.        Pool.join ()    # View execution result for    res in result:        print (Res.get ())    print ("sub-process (es) done.")

  

Python Multi-process multiprocessing

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.