Python 使用 pool.map 和列表进行多处理
Python multiprocessing using pool.map with list
我正在使用多处理编写 python 代码。下面是代码
import multiprocessing
import os
def square(n):
#logger.info("Worker process id for {0}: {1}".format(n, os.getpid()))
logger.info("Evaluating square of the number {0}".format(n))
print('process id of {0}: {1}'.format(n,os.getpid()))
return (n * n)
if __name__ == "__main__":
# input list
mylist = [1, 2, 3, 4, 5,6,7,8,9,10]
# creating a pool object
p = multiprocessing.Pool(4)
# map list to target function
result = p.map(square, mylist)
print(result)
我的服务器中CPU个核心数是4个。如果我使用4个,只会启动单个进程。一般来说,它应该启动4个独立的进程吧?
如果我在下面的 Pool 对象中将值设置为 8 就是我得到的响应
process id of 1: 25872
process id of 2: 8132
process id of 3: 1672
process id of 4: 27000
process id of 6: 25872
process id of 5: 20964
process id of 9: 25872
process id of 8: 1672
process id of 7: 8132
process id of 10: 27000
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
这启动了 5 个独立的进程(25872、8132、1672、27000、20964),即使只有 4 cpu 个核心。
我不明白为什么pool在值为4时只启动了1个进程,而在值为8时启动了5个单独的进程。
可以使用大于 CPU 核心数的值实例化池对象吗?
另外,如果列表包含一百万条记录,我们在实例化池对象时应该使用什么最佳值?
我已查阅官方 python 文档,但找不到相关信息。
请帮助
一一解答
- I don't understand why the pool initiated only 1 process when the value is 4 and initiated 5 separate processes when the value is 8.
池启动了 4 个进程。不要将您拥有的内核数量误认为是进程数量,它们是完全独立的。您有 5 个进程,因为第一个 python 进程也很重要。
因此,您从 main python 进程开始,这些进程调用池以 启动另外 4 个进程,这样就构成了其中的 5 个。
如果您看到只有少数进程正在使用,这意味着它们可能能够足够快地终止任务,因此不需要其他进程。
- Can pool object be instantiated with a value greater than the number of CPU cores?
是的,您可以实例化任何您想要的数字(尽管根据 OS 可能会有某种限制)。但请注意,这只会使您的 CPU 超载。更多解释如下。
- Also what should be the optimal value we should use while instantiating pool object if a list contains a million records?
嗯,通常 "optimal" 是您的 CPU 的所有内核都已完全被您的池使用。所以,如果你有 4 个核心,4 个进程 将是最好的选择,虽然有时这并不完全是这样,但它是一个很好的起始近似值.
最后一个音符,
I have been through official python documentation, but I couldn't find
info.
这并不是 python 具体的,它是 CS 中的一般行为。
我正在使用多处理编写 python 代码。下面是代码
import multiprocessing
import os
def square(n):
#logger.info("Worker process id for {0}: {1}".format(n, os.getpid()))
logger.info("Evaluating square of the number {0}".format(n))
print('process id of {0}: {1}'.format(n,os.getpid()))
return (n * n)
if __name__ == "__main__":
# input list
mylist = [1, 2, 3, 4, 5,6,7,8,9,10]
# creating a pool object
p = multiprocessing.Pool(4)
# map list to target function
result = p.map(square, mylist)
print(result)
我的服务器中CPU个核心数是4个。如果我使用4个,只会启动单个进程。一般来说,它应该启动4个独立的进程吧?
如果我在下面的 Pool 对象中将值设置为 8 就是我得到的响应
process id of 1: 25872
process id of 2: 8132
process id of 3: 1672
process id of 4: 27000
process id of 6: 25872
process id of 5: 20964
process id of 9: 25872
process id of 8: 1672
process id of 7: 8132
process id of 10: 27000
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
这启动了 5 个独立的进程(25872、8132、1672、27000、20964),即使只有 4 cpu 个核心。
我不明白为什么pool在值为4时只启动了1个进程,而在值为8时启动了5个单独的进程。
可以使用大于 CPU 核心数的值实例化池对象吗?
另外,如果列表包含一百万条记录,我们在实例化池对象时应该使用什么最佳值?
我已查阅官方 python 文档,但找不到相关信息。 请帮助
一一解答
- I don't understand why the pool initiated only 1 process when the value is 4 and initiated 5 separate processes when the value is 8.
池启动了 4 个进程。不要将您拥有的内核数量误认为是进程数量,它们是完全独立的。您有 5 个进程,因为第一个 python 进程也很重要。 因此,您从 main python 进程开始,这些进程调用池以 启动另外 4 个进程,这样就构成了其中的 5 个。 如果您看到只有少数进程正在使用,这意味着它们可能能够足够快地终止任务,因此不需要其他进程。
- Can pool object be instantiated with a value greater than the number of CPU cores?
是的,您可以实例化任何您想要的数字(尽管根据 OS 可能会有某种限制)。但请注意,这只会使您的 CPU 超载。更多解释如下。
- Also what should be the optimal value we should use while instantiating pool object if a list contains a million records?
嗯,通常 "optimal" 是您的 CPU 的所有内核都已完全被您的池使用。所以,如果你有 4 个核心,4 个进程 将是最好的选择,虽然有时这并不完全是这样,但它是一个很好的起始近似值.
最后一个音符,
I have been through official python documentation, but I couldn't find info.
这并不是 python 具体的,它是 CS 中的一般行为。