When running threads using the threading and Queue libraries, I am creating an object in the threads run() function. Let's say that the class initiates like so:
class Test():
def __init__(self, dictionary={}):
self.dict = dictionary
When I try to access the dictionary in different threads, it seems like Python is only creating one instance of the dictionary. Here is the Thread class. I started two of these:
class ThreadTest(threading.Thread):
def run(self):
while True:
// interact with Queue items
obj = Test()
print "%s - %s" % (self.name, id(obj.dict))
queue.task_done()
And the output:
Thread-1 - 19219616
Thread-2 - 19219616
This is really confusing because the Test class is created twice, but it is sharing the same dictionary.
Is there a way to create a new instance of dictionary in this case?