I've written a couple very simple in/out python scripts in the past to do things like turn on an LED when a button is pushed or turn on a fan if the CPU temp is too high. I noticed that one I was using had really high CPU usage (like 18%) when it was running in the background, it just watched a GPIO pin to see when a button was pushed and then executed a shutdown command.
So my question is, what's considered good practice to write python scripts to run in the background? Say I just want to light an LED when CPU temps are high. I could do something really simple like:
while True:
if(tempsHigh == True)
ledON()
else
ledOFF()
This will check the temps as fast as possible, which, depending on what it's actually doing, can be quite intensive. I could add a time.sleep(1) in the loop so it only checks once per second, but how intensive is a sleep command? Is it just checking something else, like a timer, as fast as possible and still bogging down the CPU? I could make my own sleep function, something like this:
while True:
while(currentTime < targetTime) {
pass
}
targetTime = currentTime + 1
if(tempsHigh == True)
ledON()
else
ledOFF()
But then again I'm just checking the time as fast as possible.
Basically I just want to know what's the 'best' way to simple background scripts like this so they use as little resources as possible.
As far as 'do your own research' I've seen it done with and without sleep, with and without pass loops, etc, so maybe it's the case that it really doesn't matter. I'm sure I could figure it out myself by delving into exactly what a sleep does, how a pass works, how CPUs handle delays and threading and resources, etc, but that's more than I'm looking to learn at the moment.
Edit: IDK how to do code blocks on reddit...
submitted by /u/Darkextratoasty
[link] [comments]
More...
Writing a lightweight background python script
The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries.
Post Reply
1 post
• Page 1 of 1
- /RaspberryPi
- Corporate
- Posts: 2449
- Joined: Wed Jun 05, 2019 1:29 am
Post Reply
1 post
• Page 1 of 1
Return to “Raspberry Pi Forum”
Jump to
- From Retro to Current
- ↳ Classified Ads
- ↳ Community
- ↳ Marketplace
- ↳ Jobs
- ↳ Dating
- ↳ Housing
- Nintendo Forums
- ↳ SNES Classic Edition Forum
- ↳ Switch Forum
- ↳ NES Classic Edition Forum
- ↳ Wii U Forum
- ↳ 3DS Forum
- ↳ Wii Forum
- ↳ DS Forum
- ↳ Gamecube Forum
- ↳ Game Boy Advance Forum
- ↳ Game Boy Color Forum
- ↳ Nintendo 64 Forum
- ↳ Super Nintendo Forum
- ↳ Game Boy Forum
- ↳ Nintendo Entertainment System Forum
- ↳ Game & Watch Forum
- SONY Forums
- ↳ PlayStation 5 Forum
- ↳ PlayStation Classic Forum
- ↳ PlayStation 4 Forum
- ↳ PlayStation Vita Forum
- ↳ PlayStation 3 Forum
- ↳ PlayStation Portable Forum
- ↳ PlayStation 2 Forum
- ↳ PlayStation 1 Forum
- Microsoft Forums
- ↳ Xbox Series Forum
- ↳ Xbox One Forum
- ↳ Xbox 360 Forum
- ↳ Xbox Forum
- SEGA Forums
- ↳ Genesis Mini Forum
- ↳ Dreamcast Forums
- ↳ Sega Saturn Forum
- ↳ Genesis Forum
- ↳ Master System Forum
- Other System Forums
- ↳ General Discussion
- ↳ Other Console Forum
- ↳ Arcade Cabinet Forum
- ↳ Windows, Mac, & Linux Forum
- ↳ Atomiswave Forums
- ↳ WWW.ATOMISWAVE.NET
- ↳ General Discussion
- ↳ Tech Support
- ↳ System Setups
- ↳ Modified Moderboards
- ↳ Home Consoles & Systems
- ↳ Arcade & Candy Cabinets
- ↳ Marketplace
- ↳ Video Game Marketplace
- ↳ Game Discussion
- ↳ Video Game Catalogs
- ↳ FAQs, Reviews, & Guides
- ↳ Assorted Discussion
- ↳ Custom Ideas
- ↳ Open Discussion
- ↳ SNK Neo Geo Forums
- ↳ General Discussion
- ↳ Buying & Selling
- ↳ Tech Support
- ↳ Systems
- ↳ Neo Geo Arcade Stick Pro Forum
- ↳ Neo Geo Mini Forum
- ↳ Neo Geo X Forum
- ↳ Neo Geo CD Forum
- ↳ Neo Geo MVS Forum
- ↳ TurboGrafx-16 Forums
- ↳ General Discussion
- ↳ Marketplace
- ↳ Magazine Scans
- ↳ Games
- ↳ TurboChip/HuCard Forum
- ↳ CD-ROM²/Super CD-ROM²/Arcade CD-ROM² Forum
- ↳ Systems
- ↳ TurboGrafx-16 Mini Forum
- ↳ TurboDuo Forum
- ↳ TurboExpress/PC Engine GT/LT Forum
- ↳ SuperGrafx Forum
- ↳ TurboGrafx-CD/PC Engine CD Forum
- ↳ TurboGrafx-16/PC Engine Core Forum
- ↳ Raspberry Pi Forum
- ↳ Atari Forum