Dec-19-2018, 08:32 AM
Hello, some thought
I believe too things are imbricated in your program.
1 - It does loop and then after(5,loop), which stack again your loop into the system, probably increasing the stack, which is wrong.
2 - In "loop()", an "except: prgloop()" which does the same "after(5,loop)". If an exception occurs, it will stacked, and if a new one occurs, you will reach eventually the python exception stack limit. Not very important because you will be notified.
3 - If I'm good, "after(5,loop)" means you start the loop again after 5ms. It's very few, and it could be the reason of your cpu time waste. I don't know Tk, but in the GUI I know, I never do that because... it does use a lot of CPU for nothing. The purpose of a GUI system is to react after a GUI event, not to process some other system, like for instance a serial device. Anyway, you stack a new loop every 5ms, and this is wrong. You can get a proof of that if the free memory of your system decreases regularly (try htop utility on your RPI, in the standard repository). If it does not, I'm wrong, but I'm sure it cannot be a good practice.
What you can do is :
- change your code to make the GUI starting events as it wants and terminate the event processing as quickly as possible, and don't start something long at the end of this event process.
- process the serial line in another thread, which will live alone and won't use too much the CPU. There are excellent threading modules coming with python ; choose one compatible with Tk. Does somebody has any hint about that ?. You can look and post in the GUI forum.
- If you don't have any other solution, increase the 5ms (try for instance 20ms). You can also try the time module (ex: time.sleep(0.2)) which is good to give your system some breath.
I believe too things are imbricated in your program.
1 - It does loop and then after(5,loop), which stack again your loop into the system, probably increasing the stack, which is wrong.
2 - In "loop()", an "except: prgloop()" which does the same "after(5,loop)". If an exception occurs, it will stacked, and if a new one occurs, you will reach eventually the python exception stack limit. Not very important because you will be notified.
3 - If I'm good, "after(5,loop)" means you start the loop again after 5ms. It's very few, and it could be the reason of your cpu time waste. I don't know Tk, but in the GUI I know, I never do that because... it does use a lot of CPU for nothing. The purpose of a GUI system is to react after a GUI event, not to process some other system, like for instance a serial device. Anyway, you stack a new loop every 5ms, and this is wrong. You can get a proof of that if the free memory of your system decreases regularly (try htop utility on your RPI, in the standard repository). If it does not, I'm wrong, but I'm sure it cannot be a good practice.
What you can do is :
- change your code to make the GUI starting events as it wants and terminate the event processing as quickly as possible, and don't start something long at the end of this event process.
- process the serial line in another thread, which will live alone and won't use too much the CPU. There are excellent threading modules coming with python ; choose one compatible with Tk. Does somebody has any hint about that ?. You can look and post in the GUI forum.
- If you don't have any other solution, increase the 5ms (try for instance 20ms). You can also try the time module (ex: time.sleep(0.2)) which is good to give your system some breath.