Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local variable referenced before assignment, using multi-threading

Code to add and delete values in a list are operations performed in different threads.

using these global variables in multi-threading:

from threading import Thread
import time

a=[]
i = 0
j = 0

function for thread1:

def val_in():
    while 1:
        a.append(raw_input())
        print "%s value at %d: %d added" % ( time.ctime(time.time()), i ,int(a[i])) // line 14
        i+=1

function for thread 2:

def val_out():
    while 1:
        time.sleep(5)
        try:
            print "%s value at %d: %d deleted" % (time.ctime(time.time()), j, int(a.pop(j)))
            i-=1
        except:
            print"no values lefts"
        time.sleep(2)

defining and starting threads:

t = Thread(target = val_in)
t1 = Thread(target= val_out)
t.start()
t1.start()

Now when input is given as 1, it should be added to the list a, but there is an error:

Error: Exception in thread Thread-1:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
   self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
   self.__target(*self.__args, **self.__kwargs)
  File "/Users/dhiraj.agarwal/Documents/workspace/try3/multithread.py", line 14, in val_in
UnboundLocalError: local variable 'i' referenced before assignment
like image 572
Dhiraj Agarwal Avatar asked May 21 '15 09:05

Dhiraj Agarwal


People also ask

How do I fix local variable might be referenced before assignment?

The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.

Are local variables shared between threads?

Tip: Unlike class and instance field variables, threads cannot share local variables and parameters. The reason: Local variables and parameters allocate on a thread's method-call stack. As a result, each thread receives its own copy of those variables.

Do multiple threads share global variables?

Threads share all global variables; the memory space where global variables are stored is shared by all threads (though, as we will see, you have to be very careful about accessing a global variable from multiple threads). This includes class-static members!

Can all threads access global variables?

But to answer your question, any thread can access any global variable currently in scope. There is no notion of passing variables to a thread. It has a single global variable with a getter and setter function, any thread can call either the getter or setter at any time.


1 Answers

You should tell python that i is global:

def val_in():
    global i
    ...

def val_out():
    global i
    ...
like image 160
kostya Avatar answered Oct 13 '22 14:10

kostya