pyhon multiprocess
The multiprocessing module in Python is used for creating and managing processes. It allows you to run multiple processes concurrently, each with its own separate memory space. This can be useful for tasks that can be parallelized, such as performing calculations on large datasets or running multiple independent tasks simultaneously.
Here is an example of how to use the multiprocessing module in Python:
import multiprocessing
# Define a function that will be executed by each process
def square(numbers):
result = []
for num in numbers:
result.append(num ** 2)
return result
if __name__ == '__main__':
# Create a list of numbers
numbers = [1, 2, 3, 4, 5]
# Create a multiprocessing Pool with 4 processes
pool = multiprocessing.Pool(processes=4)
# Divide the list of numbers into chunks for each process to handle
chunk_size = len(numbers) // 4
chunks = [numbers[i:i+chunk_size] for i in range(0, len(numbers), chunk_size)]
# Apply the square function to each chunk of numbers using the multiprocessing Pool
results = pool.map(square, chunks)
# Print the results
print(results)
In this example, we define a function square that takes a list of numbers as input and returns a new list with the square of each number. We then create a multiprocessing Pool with 4 processes using multiprocessing.Pool(processes=4). We divide the list of numbers into 4 chunks and apply the square function to each chunk using the map method of the multiprocessing Pool. The map method distributes the chunks of data among the available processes and returns the results as a list. Finally, we print the results.
Note that the if __name__ == '__main__': block is used to ensure that the code inside it is only executed when the script is run directly and not when it is imported as a module. This is necessary for the multiprocessing module to work correctly
原文地址: http://www.cveoy.top/t/topic/ib7S 著作权归作者所有。请勿转载和采集!