Python: Process Pool

Source: Internet
Author: User

1. Can the process be opened indefinitely?

The process cannot be opened indefinitely, the CPU has 16 cores, and the delegate can handle 16 tasks at the same time, if there are 160 processes, each process

Processing 160 processes with a processor that requires a 0.01s,16 core requires 0.5s, because the more processes it takes, the more time is needed.

2, Process Pool

ImportOSImport TimeImportRandom fromMultiprocessingImportPool fromMultiprocessingImportProcessdeffunc (i): I+ = 1if __name__=='__main__': P= Pool (5)#5 processes were created. In general, the value here is the number of #+1, if you do not know the number of cores of the CPU, you can call Os.cpu_count () to viewStart =time.time () p.map (Func,range (1000))#target = func Args=next (iterable) # [(), 1,2,3,4]P.close ()#is not allowed to add tasks to the process pool againP.join ()Print(Time.time ()-start) Start=time.time () L= []     forIinchRange (1000): P= Process (target=func,args= (i,))#100 Processes createdP.start () l.append (P) [I.join () forIinchL]Print(Time.time ()-start)

3,

Import Time fromMultiprocessingImportPool#Applydeffunc (i): Time.sleep (1) I+ = 1#print (i)    returnI+1if __name__=='__main__': P= Pool (5) res_l= []     forIinchRange (20):        #p.apply (func,args= (i)) # Apply is a mechanism for synchronizing commit tasksres = P.apply_async (func,args= (i,))#Apply_async is the mechanism for submitting tasks asynchronouslyres_l.append (RES)#print (Res.get ()) # Blocking: Waiting for task resultsP.close ()#close must be added to the join, and no new tasks are allowed.P.join ()#wait for the child process to end and then execute down[Print(I.get ()) forIinchRes_l]

Python: 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.