Hi forum,
searching through the internet gave me the impression that I lack the right search terms for my problem. I may not even know enough python to ask the right question. But I trust in you.
I would like to have a
python program do its webserver background chores with
flask and a second thread that checks the
serial input for data. When that serial data happens to add up to a meaningful message, it shall be transferred to the main thread and update the web site.
I have not yet seen events, subscriptions or event handlers as I know them from C#. I would have gone single-threaded if I had. But I seem to have to use a blocking
Serial.Read() and let it hang in a separate thread.
Then I would like to create an event have an event handler that runs in the main thread (InvokeRequired(), anyone?). But python doesn't seem to work that way.
So my basic question: Does python even run an event loop that the humble programmer can use and I am too blind to find it?
Or would it be OK to just run my event handler from the serial-input-watching thread and there is no pitfall to this?
What I have tried:
def __init__(self, commPort:Serial):
self._commPort = commPort
self._inputMessageQueue = list()
self._commThread = threading.Thread(daemon=True, target=self._ObserveComm, args=(self,))
def _ObserveComm(self):
while(True):
oneByte = bytearray(self._commPort.read(1))
if(len(oneByte) > 0):
oneMessage = Message.Parse(oneByte)
if(issubclass(type(oneMessage), Message)):
self._inputMessageQueue.append(oneMessage)