您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在不处于顶层的情况下解决python多处理的酸洗错误?

5b51 2022/1/14 8:22:09 python 字数 2700 阅读 509 来源 www.jb51.cc/python

我已经多次研究过这个问题,但是没有找到一个可以在我的情况下工作的解决方法,或者我理解的解决方法,所以请耐心等待. 基本上,我有一个功能的层次组织,这阻止我在顶层进行多处理.不幸的是,我不相信我可以改变程序的布局 – 因为我需要在初始输入后创建的所有变量. 例如,说我有这个: import multiprocessing def calculate(x): # here is wher

概述

基本上,我有一个功能的层次组织,这阻止我在顶层进行多处理.不幸的是,我不相信我可以改变程序的布局 – 因为我需要在初始输入后创建的所有变量.

例如,说我有这个:

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多处理的酸洗错误?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶