0

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 from the database.

I'm having issues with the variables being referenced before assignment.

import threading

class Data():
        global a
        global b
        a = 1
        b = 1



class A(threading.Thread):
    def run(self):
        while True:
            a += 1


class B(threading.Thread):
    def run(self):
        while True:
            print a
            print b
            b -= 1


a_thr = A()
b_thr = B()
a_thr.start()
b_thr.start()

1 Answer 1

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.

class A(threading.Thread):

    def run(self):
        global a
        global b
        while True:
            a += 1



class B(threading.Thread):

    def run(self):
        global a
        global b
        while True:
            print a
            print b
            b -= 1
Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.