多重处理:在某些情况下会在赋值之前引用变量,但在其他情况下不会 [英] multiprocessing: variable being referenced before assignment in some cases but not others
问题描述
我在此网站的某个地方找到了以下示例:
I found the following example on this website somewhere:
import multiprocessing
import ctypes
import numpy as np
shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape(10, 10)
# No copy was made
assert shared_array.base.base is shared_array_base.get_obj()
# Parallel processing
def my_func(i, def_param=shared_array):
shared_array[i,:] = i
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
pool.map(my_func, range(10))
print shared_array
上面的代码可以正常工作,但是如果我想向共享数组中添加一个数组,就像shared_array + = some_other_array(而不是上面的shared_array [i ,;] = i),我得到
The above code works fine, but if I want to add an array to the shared array, something like shared_array += some_other_array (instead of the above shared_array[i,;] = i) I get
分配前引用的本地变量'shared_array'
local variable 'shared_array' referenced before assignment
我为什么不能做到这一点的任何想法?
Any ideas why I cannot do that?
推荐答案
如果将变量分配给函数中的任何位置,则将其视为局部变量. shared_array += some_other_array
等效于shared_array = shared_array + some_other_array
.因此,shared_array
被视为局部变量,当您尝试在赋值的右侧使用它时,该变量不存在.
If a variable is assigned to anywhere in a function, it is treated as a local variable. shared_array += some_other_array
is equivalent to shared_array = shared_array + some_other_array
. Thus shared_array
is treated as a local variable, which does not exist at the time you try to use it on the right-hand side of the assignment.
如果要使用全局shared_array
变量,则需要通过在函数中放置global shared_array
来将其明确标记为全局变量.
If you want to use the global shared_array
variable, you need to explicitly mark it as global by putting a global shared_array
in your function.
您没有看到shared_array[i,:] = i
错误的原因是它没有分配给变量shared_array
.相反,它将使该对象变异,并分配给它的一部分.在Python中,分配裸名(例如shared_array = ...
)与任何其他类型的分配(例如shared_array[...] = ...
)都大不相同,即使它们看起来很相似.
The reason you don't see the error with shared_array[i,:] = i
is that this does not assign to the variable shared_array
. Rather, it mutates that object, assigning to a slice of it. In Python, assigning to a bare name (e.g., shared_array = ...
) is very different from any other kind of assignment (e.g., shared_array[...] = ...
), even though they look similar.
请注意,该错误与多处理无关.
Note, incidentally, that the error has nothing to do with multiprocessing.
这篇关于多重处理:在某些情况下会在赋值之前引用变量,但在其他情况下不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!