python - multiprocessing: variable being referenced before assignment in some cases but not others -
I found the following example on this site somewhere:
import multi import ctypes import numpy NP shared_array_base = multiprocessing.Array (ctypes.c_double, built 10 * 10) shared_array = np.ctypeslib.as_array (shared_array_base.get_obj ()) shared_array = shared_array.reshape (10, 10) as # not copy loud shared_array.base Was there. Based 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 (procedures = 4) pool.map ( my_func, (10)) print shared_array the above code works fine, but I want to add an array of shared array, some (like shared_array + = some_other_array Instead of shared_array [i,;] = i) i
local variables referred to before 'shared_array' assignment
Any reason is reason that I Can not?
is that a variable is assigned anywhere in a function, it is regarded as a local variable Is shared_array + = some_other_array equals shared_array = shared_array + some_other_array . Thus shared_array is considered as a local variable, which is not present at the time when you try to use it on the right side of the assignment. If you want to use the global shared_array variable, you must explicitly mark it by entering a global share_re in your function. This is because it does not specify that gives shared_array [i,:] = i This variable error that with shared_array appear. Instead, it changes the object that tells a piece of it. In Python, a bare name (for example, shared_array = ... ) is different from another type of assignment (for example, shared_array [...] = ... ), although they look similar. Note, incidentally, there is nothing with multi-generator of error.
Comments
Post a Comment