Here is the script that cause me a problem: 3 Threads : T1, S1, S2, and 2 queues. T1 sends context variable to q_Strategy_1 and q_Strategy_2. S1 and S2 read respectively context from q_Strategy_1 and q_Strategy_2. S1 and S2 add some info to context and print it.
Expected output is :
{'Var_1': 'a', 'Var_2': 'b'}
{'Var_1': 'a', 'Var_2': 'b', 'Var_3': 'c'}
{'Var_1': 'a', 'Var_2': 'b', 'Var_4': 'd'}
While script bellow returns
{'Var_1': 'a', 'Var_2': 'b'}
{'Var_1': 'a', 'Var_2': 'b', 'Var_3': 'c'}
{'Var_1': 'a', 'Var_2': 'b', 'Var_3': 'c', 'Var_4': 'd'}
It is probably a syntax error on how i am using variable / class / threads but i can't find it :(
import threading
import time
import queue
#setup queue environment
q_Strategy_1 = queue.Queue(100)
q_Strategy_2 = queue.Queue(100)
class T1(object):
def __init__(self, name=None):
self.name = name
thread = threading.Thread(target=self.run, args=())
thread.start()
self.__context = {}
def run(self):
while True:
self.__context = {'Var_1':"a",'Var_2':"b"}
print(self.__context)
q_Strategy_1.put(self.__context)
q_Strategy_2.put(self.__context)
time.sleep(3)
class S1(object):
def __init__(self, name=None):
self.name = name
thread = threading.Thread(target=self.run, args=())
thread.start()
self.__context = {}
def run(self):
while True:
self.__context = {}
if not q_Strategy_1.empty():
self.__context = q_Strategy_1.get()
self.__context['Var_3'] = "c"
print(self.__context)
class S2(object):
def __init__(self, name=None):
self.name = name
thread = threading.Thread(target=self.run, args=())
thread.start()
self.__context = {}
def run(self):
while True:
self.__context = {}
if not q_Strategy_2.empty():
self.__context = q_Strategy_2.get()
self.__context['Var_4'] = "d"
print(self.__context)
T1 = T1(name='T1')
S1 = S1(name='S1')
S2 = S2(name='S2')