Skip to content Skip to sidebar Skip to footer

Python: Global Variables In Threads

I am trying to make my program work offline. The way i decided to to this is to have the main application work from memory in its own thread, whilst another thread read/writes data

Solution 1:

This does not have anything to do with threads. Setting

global variable

does not set this variable as global everywhere, but only in that function. Just add my changes to your code and it should run.

classA(threading.Thread):

    defrun(self):
        global a
        global b
        whileTrue:
            a += 1classB(threading.Thread):

    defrun(self):
        global a
        global b
        whileTrue:
            print a
            print b
            b -= 1

Post a Comment for "Python: Global Variables In Threads"