[python little knowledge] python parallel computing with the same function

In Python, we can use a variety of ways to achieve parallel computing, such as using multi-processes, multi-threads, and coroutines. In this article, we will focus on how to use Python's built-in module multiprocessing to achieve parallel computing of the same function, with detailed code and explanations.

 The multiprocessing module is a module in the Python standard library to provide cross-platform multi-processing support. By using this module, we can easily implement parallel computing and improve the operating efficiency of the program.

Below we will use a simple example to demonstrate how to use the multiprocessing module to implement parallel computing of the same function. The function of this function is to calculate the square of an integer x.

Code:

The first step is to import the multiprocessing module.

import multiprocessing

The second step is to define a function square.

def square(x):
    return x ** 2

The third step is to define a function parallel_compute, which accepts two parameters, an integer n and an integer num_processes, respectively representing the number of numbers to be calculated and the number of processes used.

def parallel_compute(n, num_processes):
    # 计算每个进程要处理的数据量
    chunk_size = n // num_processes

    # 创建进程池
    pool = multiprocessing.Pool(num_processes)

    # 计算结果
    results = []
    for i in range(num_processes):
        start = i * chunk_size + 1
        end = (i + 1) * chunk_size
        if i == num_processes - 1:
            end = n
        results.append(pool.apply_async(compute_chunk, args=(start, end)))

    # 获取结果
    values = []
    for r in results:
        values.extend(r.get())
    return values

In this function, we first calculate the amount of data to be processed by each process, that is, distribute n numbers equally to num_processes processes. Then, we create a pool of processes and assign each process a computing task. Finally, we wait for the calculation results of all processes and combine them into one list.

The fourth step is to define an auxiliary function compute_chunk, which accepts two parameters, an integer start and an integer end, respectively representing the start value and end value of the data to be calculated.

def compute_chunk(start, end):
    return [square(x) for x in range(start, end + 1)]

In this function, we call the square function to calculate the square of each number and save the calculated result in a list.

The fifth step is to test the code.

if __name__ == '__main__':
    n = 100
    num_processes = 4
    result = parallel_compute(n, num_processes)
    print(result)

In the main program, we define the number n of numbers to be calculated and the number of processes num_processes to use. Then call the parallel_compute function to calculate and output the result.

Running the above code, we can get the following results:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]

It can be seen from the above results that we have successfully implemented the parallel calculation of the same function using the multiprocessing module, and obtained correct calculation results.

Summarize:

In this article, we introduced how to use Python's built-in module multiprocessing to implement parallel computing of the same function. Through the demonstration of the above code, we can see that the module is very simple and convenient to use, and can greatly improve the operating efficiency of the program. If you need to perform computationally intensive tasks, try using the multiprocessing module to achieve parallel computing.

Guess you like

Origin blog.csdn.net/wq10_12/article/details/132040533