Python: Process pool, python process pool

Source: Internet
Author: User

Python: Process pool, python process pool

1. Can a process be opened infinitely?

Processes cannot be opened infinitely. The cpu has 16 cores, which means that 16 tasks can be processed simultaneously. If there are 160 processes, each process

0.01 s for processing, and 160 s for a 16-core processor to process 0.5 processes, because the more processes, the more time required.

2. Process pool

Import osimport timeimport randomfrom multiprocessing import Poolfrom multiprocessing import Processdef func (I): I + = 1if _ name _ = '_ main _': p = Pool (5) # Five processes are created. Generally, the value here is the number of cores # + 1. If you do not know the number of cpu cores, you can call the OS. cpu_count () view start = time. time () p. map (func, range (1000) # target = func args = next (iterable) # [(, 3),] p. close () # It is not allowed to add task p to the process pool. join () print (time. time ()-start) start = time. time () l = [] for I in range (1000): p = Process (target = func, args = (I,) # created one hundred processes p. start () l. append (p) [I. join () for I in l] print (time. time ()-start)

3,

Import timefrom multiprocessing import Pool # applydef func (I): time. sleep (1) I + = 1 # print (I) return I + 1if _ name _ = '_ main _': p = Pool (5) res_l = [] for I in range (20): # p. apply (func, args = (I,) # apply is the mechanism for synchronously submitting tasks res = p. apply_async (func, args = (I,) # apply_async is the mechanism for asynchronous task submission res_l.append (res) # print (res. get () # blocking: waiting for the task result p. close () # close must be added to join. New tasks cannot be added to p. join () # Wait until the sub-process ends and execute [print (I. get () for I in res_l]

 

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.