【发布时间】:2015-05-03 12:29:09
【问题描述】:
我已经多次研究过这个问题,但没有找到适合我或我理解的解决方法,所以请多多包涵。
基本上,我有一个分层的功能组织,这阻止了我在顶层进行多处理。不幸的是,我不相信我可以改变程序的布局——因为我需要在初始输入之后创建的所有变量。
例如,假设我有这个:
import multiprocessing
def calculate(x):
# here is where I would take this input x (and maybe a couple more inputs)
# and build a larger library of variables that I use further down the line
def domath(y):
return x * y
pool = multiprocessing.Pool(3)
final= pool.map(domath, range(3))
calculate(2)
这会产生以下错误:
Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
我在考虑全局变量,但我担心我必须定义太多,这可能会大大降低我的程序速度。 有没有无需重新构建整个程序的解决方法?
【问题讨论】:
-
您可能想阅读我在尝试解决pretty much the same error时选择的答案。
-
嘿肖恩,在看完你的问题后,恐怕我的解决方案可能超出了我的理解范围。无论如何,您可以给我一个更概念性的关于这些函数在打包和解包时的作用吗?
-
当然。让我一起获取一些示例代码。
标签: python multiprocessing pickle python-multiprocessing