gpt4 book ai didi

python - python 是否有一个简单的基于进程的并行映射?

转载 作者:IT老高 更新时间:2023-10-28 20:22:13 27 4
gpt4 key购买 nike

我在找一个简单的python的基于进程的并行映射,也就是一个函数

parmap(function,[data])

这将在不同进程上的 [data] 的每个元素上运行函数(嗯,在不同的核心上,但是 AFAIK,在 python 中在不同核心上运行东西的唯一方法是启动多个解释器),并返回一个结果列表。

这样的东西存在吗?我想要简单的东西,所以一个简单的模块会很好。当然,如果不存在这样的东西,我会满足于一个大图书馆:-/

最佳答案

我觉得你需要的是 map method in multiprocessing.Pool() :

map(func, iterable[, chunksize])

A parallel equivalent of the map() built-in function (it supports only
one iterable argument though). It blocks till the result is ready.

This method chops the iterable into a number of chunks which it submits to the
process pool as separate tasks. The (approximate) size of these chunks can be
specified by setting chunksize to a positive integ

例如,如果你想映射这个函数:

def f(x):
return x**2

到 range(10),你可以使用内置的 map() 函数:

map(f, range(10))

或使用 multiprocessing.Pool() 对象的方法 map():

import multiprocessing
pool = multiprocessing.Pool()
print pool.map(f, range(10))

关于python - python 是否有一个简单的基于进程的并行映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1704401/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com