Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Python
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)
Posted
Updated 16-Feb-21 4:04am

 
Share this answer
 
Comments
lukeer 9-Feb-21 14:30pm    
Yes, there are events in threading.
But as far as I understand it, I would still have to create an endless loop in the main thread that polls the event.
Did I miss the part where I can attach a function to the event? Or does python work differently?
Richard MacCutchan 11-Feb-21 9:30am    
I do not think Python has such a mechanism natively, but there may be a library that does it.
Richard MacCutchan 11-Feb-21 9:33am    
Quote:
Does python even run an event loop that the humble programmer can use and I am too blind to find it?

No, unless you add such a loop by yourself or use a library/framework that sets up an event loop for you. By itself, Python is a traditional sequential language that executes a program from beginning to end.
 
Share this answer
 
Comments
Richard MacCutchan 11-Feb-21 9:25am    
It does have an event mechanism, see the link in my post above.
markkuk 11-Feb-21 10:21am    
That's an add-on package, not part of the language specification. It's an example of "a library/framework that sets up an event loop" as I mentioned in my post. There are others, like most GUI frameworks.
Richard MacCutchan 11-Feb-21 10:41am    
Quote: Changed in version 3.7: This module used to be optional, it is now always available.
Finally opted for blinker[^] for an event system since flask used it anyway.

Thank you all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900