概述
基本上,我有一个功能的层次组织,这阻止我在顶层进行多处理.不幸的是,我不相信我可以改变程序的布局 – 因为我需要在初始输入后创建的所有变量.
例如,说我有这个:
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
我在考虑全局,但我担心我必须定义太多,这可能会使我的程序减慢很多.
是否有任何解决方法而无需重组整个计划?
>>> from pathos.multiprocessing import ProcessingPool as Pool >>> >>> def calculate(x): ... def domath(y): ... return x*y ... return Pool().map(domath,range(3)) ... >>> calculate(2) [0,2,4]
你甚至可以坚持下去……因为大多数东西都是腌制的.不需要奇怪的非pythonic解决方案,您必须使用纯多处理进行烹饪.
>>> class Foo(object): ... def __init__(self,x): ... self.x = x ... def doit(self,y): ... return ProcessingPool().map(self.squared,calculate(y+self.x)) ... def squared(self,z): ... return z*z ... >>> def thing(obj,y): ... return getattr(obj,'doit')(y) ... >>> ProcessingPool().map(thing,ProcessingPool().map(Foo,range(3)),range(3)) [[0,0],[0,4,16],16,64]]
获取悲情:https://github.com/uqfoundation
总结
以上是编程之家为你收集整理的如何在不处于顶层的情况下解决python多处理的酸洗错误?全部内容,希望文章能够帮你解决如何在不处于顶层的情况下解决python多处理的酸洗错误?所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧