Python process pool function display and example analysis

乌拉乌拉~
Release: 2018-08-23 17:41:50
Original
2125 people have browsed it

In the following article, we will learn about what is the process pool in python. Learn about the python process pool and what role the process pool can play in python programming.

Process Pool

The Pool class describes a pool of worker processes. It has several different methods for tasks to offload worker processes.

The process pool maintains a process sequence internally. When used, it goes to the process pool to obtain a process. If there is no available process in the process pool sequence, the program will wait until it is in the process pool. until there are available processes.

We can use the Pool class to create a process pool and expand the submitted tasks to the process pool.

Let’s give an example:

#apply
from  multiprocessing import Pool
import time

def f1(i):
    time.sleep(0.5)
    print(i)
    return i + 100
if __name__ == "__main__":
    pool = Pool(5)
    for i in range(1,31):
        pool.apply(func=f1,args=(i,))
        
#apply_async
def f1(i):
    time.sleep(0.5)
    print(i)
    return i + 100
def f2(arg):
    print(arg)
    
if __name__ == "__main__":
    pool = Pool(5)
    for i in range(1,31):
        pool.apply_async(func=f1,args=(i,),callback=f2)
    pool.close()
    pool.join()
Copy after login

A process pool object can control which work in the worker process pool can be submitted. It supports asynchronous results of timeouts and callbacks, and has a map-like implementation. .

processes: The number of worker processes used. If processes is None then use the number returned by os.cpu_count().

initializer: If initializer is None, then each worker process will call initializer(*initargs) at the beginning.

maxtasksperchild: The number of tasks that can be completed before the worker process exits. After completion, a new worker process will replace the original process to release idle resources. maxtasksperchild defaults to None, which means that the worker process will survive as long as the Pool exists.

context: Used to specify the context when the worker process starts. Generally, multiprocessing.Pool() or the Pool() method of a context object is used to create a pool. Both methods set the context appropriately

Note: The methods of the Pool object can only be called by the process that created the pool.

The above is all the content described in this article. This article mainly introduces the relevant knowledge of python process pool. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of Python process pool function display and example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template