I have been getting into the festive spirit and have set a code challenge to light up my tree with a get request using Flask.
Currently, I am hitting a UnboundLocalError: local variable 'test' referenced before assignment
I don't understand how to set up a variable that one thread can set to either true or false and the other reads it and lights up my tree.
import threading
from flask import Flask
app = Flask(__name__)
test = 'best'
@app.route('/')
def hello():
test = not test
return "Hello World!"
def background():
from gpiozero import LEDBoard
from gpiozero.tools import random_values
from signal import pause
from time import sleep
tree = LEDBoard(*range(2,28),pwm=True)
while True:
if test:
for led in tree:
led.source_delay = 0.1
led.source = random_values()
sleep(0.2)
def foreground():
app.run(host='0.0.0.0')
test = True
b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)
b.start()
f.start()
What is the best way for me to share the test variable and alternate it between true and false between the 2 threads without some heavy engineering such as using a database or writing out to a text file?