Sorry if this is the wrong category … the categories are not obvious to me…
Anyway, I’d like have a RaspPi run a Flask web server while simultaneously attending to GPIO events, etc. Sound like a multi threaded problem, yes? I thought so so I did the following code:
import time
import automationhat
from threading import Thread
from flask import Flask
app = Flask(__name__)
### Classes ###
class run_light:
def __init__(self):
automationhat.light.power.write(1)
self._running = True
def terminate(self):
automationhat.light.power.write(0)
self._running = False
def run(self):
while self._running:
automationhat.light.comms.toggle()
time.sleep(2)
### Functions ###
@app.route("/")
def hello():
return "Hello World!"
### MAIN ###
if automationhat.is_automation_hat():
print("Started daemon")
runlight = run_light()
runlightthread = Thread(target=runlight.run)
runlightthread.start()
print("Run light thread started")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
This causes the Comms pin to wildly flash rather than flashing every two seconds. Any ideas or techniques for running a webserver thread in parallel with a automation / GPIO servicing thread?
I’m sure this is better for StackOverflow but maybe someone knows of inherent incompatibilities to AutomationHat driver and Flash…
Thanks for any pointers!