(vii) 8-2 multi-process manager and process pool

Source: Internet
Author: User


Python provides a powerful manage for data sharing with a very large number of supported types, including Value,array,list,dict,queue,lock, etc.

For example:

Import  multiprocessingdef Worker (d,l):    l  + = xrange (1,5) for    i in Xrange (11,15):        key = "key{0}". Format (i)        val = "val{0}". Format (i)        d[key] =  val    # print D    # print Lif __name__ = = "__main__":    Manager = multiprocessing. Manager ()    d = manager.dict ()    L = manager.list ()    p = multiprocessing. Process (target=worker,args= (d,l))    P.start ()    p.join ()    print D    print  l

Operation Result:

{' key14 ': ' Val14 ', ' key13 ': ' Val13 ', ' key12 ': ' val12 ', ' key11 ': ' Val11 '} [1, 2, 3, 4]

Process Pool
Pool can provide a specified number of processes for the user to invoke, and when a new request is submitted to the pool, a new process is created to execute the request if it is not full, but if the number of processes in the pool has reached the specified maximum, the request waits until the process ends in the pool. Before a new process is created.

The difference between blocking and non-blocking:
Pool.apply_async non-blocking, the maximum number of process pool processes defined can be executed concurrently.
Pool.apply a process is finished and released back to the process pool before the next process can begin

Import Multiprocessingimport  timedef Worker (msg):    print "##### start  {0} #####". Format (msg)    Time.sleep (1)    print "#####   end  {0} #####". Format (msg) If __name__ = = "__main__":    print "main start"    pool = multiprocessing. Pool (processes=3) for    i in Xrange (1,10):        msg = "Hello {0}". Format (i)        Pool.apply_async (Func=worker,args = (msg,)) # pool.apply (func=worker,args= (msg,))    pool.close ()    pool.join ()  #join一直一定要close  , Otherwise    the error print "main End"

Operation Result:

Main start##### start Hello 1 ########## start Hello 2 ########## start Hello 3 ########## end Hello 1 ########## start he  Llo 4 ########## End Hello 2 ########## start Hello 5 ########## end Hello 3 ########## start Hello 6 ########## End Hello 4 ########## Start Hello 7 ########## end Hello 5 ########## start Hello 8 ########## end Hello 6 ########## start Hello 9 ########## End Hello 7 ########## end Hello 8 ########## End Hello 9 ########## end Hello 9 #####

  

(vii) 8-2 multi-process manager and 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.