Reputation: 1387
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()
Upvotes: 0
Views: 1217
Reputation: 612
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
Upvotes: 1