How to share and print class attributes with multi thread?

Question:

I have the program, which does stuff. And it counts how many times it has done some things by day and by hour. So I created a class and assigned it to hourly and daily.
And besides that, I have a multi thread function (let’s call it background) which is used for the menu in the console. It is used to see/print or even modify variables. But it doesn’t work. Every time I want it to print the class attributes, it always prints 0 for all attributes. How to fix this?
I also have this class and functions in separate modules
module a:

class Data():
   def __init__(self,some_count):
       self.some_count=some_count
daily=Data(0)
hourly=Data(0)

module b:

from a import daily,hourly
 
def print_data(command):
    if command == "daily" :print(f"Daily saying hi is: {daily.some_count}")
    if command == "hourly" :print(f"Hourly saying hi is: {hourly.some_count}")


background(): #It is used for menu. Depending on what you want, it can also print class attributes
    while True:
        print_data(input()) #you need to choose the command

module c:

from a import daily,hourly
from b import background

threading1 = threading.Thread(target=background)  #
threading1.daemon = True                          #
threading1.start()                                #these 3 lines are copy pasted from my code

#this is the main function. And if you insert print(hourly.some_count) it will print the right value
while True:
    hourly.some_count+=1   
    daily.some_count+=2
    time.sleep(10000)

Note, this is not my code. Well it is, but just the idea. The above code is not functional, i just wanted to show, how i coded it.

I just don’t know, why the function to print doesn’t work. I assume that the "daily" and "hourly" class are mutated for a thread?

Perhaps it is a problem with imports? I have defined a class in module a, imported the "daily" and "hourly" in class b where I used in function. And then imported that function into module c where the main program is?

Thank you for help

Asked By: Tom02

||

Answers:

your concepts are correct: instance attributes changed in one thread should be visible in another thread. What I think might be wrong in your setup has to do with module naming and importing: some of the imports are ending internally as "myproject.a" and others just as "a": internally Python will create separate modules and separate objects.

If you uniformize the module names and containing directory in a Python package, everything should work as expected.

Try this: put all your .py modules in a folder containing an empty (0 bytes) file named __init__.py – and write your entry-point (the code from which your program will start running) in a file named __main__.py. Rewrite your imports to read from .a import daily, hourly (see the ".") –
and then from the parent of that directory, run the project with python -m <directory_name> – Python should execute the code in __main__.py and see all the other modules as part of the same package.

Answered By: jsbueno
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.