简体   繁体   English

Python 多处理问题关于 function 多个 arguments 的迭代

[英]Python multiprocessing question on iteration of a function multiple arguments

I need some help on this, I have played around with multiple options on stackoverflow and internet.我需要一些帮助,我在 stackoverflow 和互联网上玩过多个选项。 But I need some help on this as I'm confused.但我需要一些帮助,因为我很困惑。 I'm on Python 2.7.我在 Python 2.7 上。

This is my manager for the multi-processing.这是我的多处理经理。 I just need to iterate function1 based on the n_iterations and collect the result per each iteration.我只需要基于 n_iterations 迭代 function1 并收集每次迭代的结果。 I have imported the two libraries,我已经导入了这两个库,

from functools import partial
import multiprocessing

Function1 is;功能1是;

def function1(v1,v2,v3,v4,v5):
    calculate_function = v1+v2+v3+v4+v5
    return calculate_function

And the function to handle the multi-processing is,而处理多处理的function是,

def multi_process(n_iterations,a1,a2,a3,a4,a5):
    sampling_process = partial(function1, v1=a1,v2=a2,v3=a3,v4=a4,v5=a5)
    pool = multiprocessing.Pool()
    results_set = pool.map(sampling_process, xrange(n_iterations)) 
    pool.close()
    pool.join()
    return results_set

But I keep getting an error message,但我不断收到一条错误消息,

  File "model_selection_pooling_ray.py", line 246, in multi_process
    results_set = pool.map(sampling_process, xrange(n_iterations)) 
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
    raise self._value
NameError: global name 'valuofv1' is not defined

(valuofv1 is the actual value on a1) Can someone please help me out on figuring out what I am doing wrong? (valueofv1 是 a1 的实际值)有人可以帮我弄清楚我做错了什么吗? Thank you.谢谢你。

the sampling_process does not need any args, you can define the Function1:采样过程不需要任何参数,您可以定义Function1:

def function1(n, v1,v2,v3,v4,v5):

I found the answer, the problem was related to the actual code calling for the function valueof1 which did not exist.我找到了答案,问题与调用不存在的 function valueof1的实际代码有关。 After fixing this, I adjusted function1 to include the iterating value as well, def function1(v1,v2,v3,v4,v5,n): to fix it.修复此问题后,我调整了 function1 以包含迭代值, def function1(v1,v2,v3,v4,v5,n):修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM