I have a function func1()
inside which I want to do a parallel job using multi-threading. I am defining another function inside that function called func_thread()
to access some local variables and avoid passing them to the thread function. However, for all those variables that are read-only, I do not have any problem. But, for the output value (x
here which all threads are adding some value to x
) I have trouble.
In this case I receive x referenced before assignment
error.
def func1():
lc = threading.Lock()
x = np.zeros((100,100))
def func_thread():
lc.acquire()
x = x + np.random.random((100,100))
lc.release()
And in this case, I receive global variable x is not defined
error!
def func1():
x = np.zeros((100,100))
def func_thread():
global x
x = x + np.random.random((100,100))