I am writing a piece of code where I have one class A and two threads B and C.
I create an instance a of A. I then start both threads, first B then C.
B calls a function func_name in A by a.func_name(). So far so fine.
C on the other hand needs to access the result which is a list, say list_a defined inside func_name() in class A and accessed by instance a.
I have to match a set of string by using a for loop like this,
if self.string_variable in a.list_a:
print "found"
but it gives me an error:
Ainstance has no attributelist_a
Can some one please help me?
You will need some kind of synchronization primitive – exactly which one depends on further details of your design and requirements.
Assuming the list
a.list_bis to be created once and is not modified later, threadCneeds to wait untila.func_name()returns. This can be achieved by adding athreading.Eventinstance toA. InA.__init__(), addAt the end of
A.func_name(), addBefore thread
Ctries to accessa.list_b, addto wait until
a.func_name()has finished in threadB.In general, synchronization between threads is a complex topic and an error-prone task. You should only do this if you really need to.