Python's multi-process pool

Source: Internet
Author: User
  1. Mulitprocessing. Pool initialization parameter to specify the number of threads in the process pool
  2. Add Task Obj.apply_async (func, (ARG)) to the process pool
  3. You need to call the Close method of the process pool after you finish adding tasks
  4. The pool process is implemented in such a way that the main process does not wait, shuts down after execution, and causes the child process to not execute, so the join method needs to be called
"""python 进程池"""from multiprocessing import Process, Poolimport os# create a new process pool with arg 2 means the process pool contain 2 processpool = Pool(2)# 使用多进程执行的任务def test(arg):    print("%d---work,work...this is %s process"%(arg, os.getpid()))for i in range(20):    print("putting job %d"%i)    pool.apply_async(test, (i,))  # async异步执行pool.close()  # 关闭进程池,相当于 不能够再次添加新任务了pool.join()# 当主进程的任务做完之后 立马结束,,,如果这个地方没join,会导致# 进程池中的任务不会执行print("main process runing.....")

Python's multi-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.