0

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')
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
richard
  • 1
  • 2
  • Can you clarify what is the problem? The output for me is just ``{'BBBBBB': 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'}`` repeated infinitely; there is no data from ``Strategy_1_Thread`` being printed. Please see the [mcve] and [ask] page how to best help us help you. – MisterMiyagi Sep 15 '20 at 08:50
  • Thanks - just did it ! – richard Sep 15 '20 at 11:47
  • You are explicitly sharing the ``self.__context`` attributes by ``put``ing and ``get``ing them into/from the queues. Both ``q_Strategy_1`` and ``q_Strategy_2`` end up with the ``self.__context`` from ``T1``, and both ``S1`` and ``S2`` store this as their ``self.__context``. Did you intend to *copy* the context's before sharing them? – MisterMiyagi Sep 15 '20 at 12:11
  • Yes T1 is sending context to q_Strategy_1 and q_Strategy_2. They both receive {'Var_1':"a",'Var_2':"b"}. S1 is pulling q_Strategy_1 and is adding ['Var_3'] = "c" to its own context dict and q_Strategy_2 and is adding ['Var_4] = "d" to its own dict as well. However what i get is {'Var_1': 'a', 'Var_2': 'b', 'Var_3': 'c', 'Var_4': 'd'} out of S2, so there is somethig mixed up somewhere... – richard Sep 15 '20 at 13:20
  • "to its own context dict" there is no such thing. There is a single dict stored on all three objects. Are you aware that assignment does not create a copy? – MisterMiyagi Sep 15 '20 at 13:59
  • so when i have for each class a self.__context it is the same object as the one in the other class ? So I need to create a copy each time ? – richard Sep 15 '20 at 14:06
  • Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – MisterMiyagi Sep 15 '20 at 14:14
  • It did ! Thank you very much ! – richard Sep 15 '20 at 14:23
  • Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – Tomerikoo Sep 15 '20 at 14:26

0 Answers0