Python multi-process multiprocessing, multiprocessing

Source: Internet
Author: User

Python multi-process multiprocessing, multiprocessing

Import multiprocessingimport time # A specific processing function that processes 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, and execute the pool = multiprocessing in parallel. pool (processes = 4) # Add the running result to a list and follow the execution result of each process. result = [] # produce msg, and add the process pool for I in range (10): msg = "hello % d" % (I) # apply_async. It is non-blocking and supports result return for callback result. append (pool. apply_async (func, (msg,) # Shut down the process Pool so that it does not accept the new job pool. close () # The main process blocks and waits for the child process to exit. 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 .")

Related Article

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.