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

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

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

我在考虑全局,但我担心我必须定义太多,这可能会使我的程序减慢很多.
是否有任何解决方法而无需重组整个计划?

解决方法 您可以使用pathos.multiprocessing,它是使用dill序列化程序而不是pickle的多处理分支. dill可以在python中序列化几乎任何东西.然后,无需编辑您的代码.
>>> 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多处理的酸洗错误?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1206504.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存