dev-resources.site
for different kinds of informations.
Automate Saving the Planet... Or Just Your Computer's Energy 🐍
Published at
11/19/2024
Categories
devto
discuss
jokes
python
Author
wladradchenko
Author
13 person written this
wladradchenko
open
I came across @oliverbennet is post "What's a Quickie Post?" and couldn’t resist giving it a try Quickie Post myself. So, here’s a Python twist on a global problem:
Problem: Fighting climate change is a serious challenge, but did you know Python is playing a role in it?
Imagine you’re writing code on Python to help reduce energy waste:
import os
def save_energy():
if os.name == "nt": # Windows
os.system("shutdown /s /t 1")
elif os.name == "posix": # macOS/Linux
os.system("pmset sleepnow")
else:
print("Unsupported OS! 🌍💻")
print("Computer shutting down to save energy! 🌱")
# Save the planet
save_energy()
Great, let's try something more sophisticated. We’re all aware that high CPU or GPU usage translates to more energy consumption. So, this Python script uses APScheduler to periodically check system load, and if it’s too high, it shuts down or puts the computer to sleep. 🌍
import os
import psutil # For monitoring CPU usage
import GPUtil # For monitoring GPU usage
from apscheduler.schedulers.blocking import BlockingScheduler
def save_energy():
"""Shutdown or put the system to sleep if CPU or GPU load is too high."""
print("Energy-saving monitor started! 🌿")
if os.name == "nt": # Windows
os.system("shutdown /s /t 1")
elif os.name == "posix": # macOS/Linux
os.system("pmset sleepnow")
else:
print("Unsupported OS! 🌍💻")
print("Computer shutting down to save energy! 🌱")
def check_system_load():
"""Check CPU and GPU load and trigger save_energy if either is too high."""
# Check CPU load
cpu_load = psutil.cpu_percent(interval=1)
print(f"Current CPU Load: {cpu_load}%")
# Check GPU load
gpus = GPUtil.getGPUs()
if gpus:
gpu_load = max(gpu.load * 100 for gpu in gpus) # Get max load of all GPUs
print(f"Current GPU Load: {gpu_load}%")
else:
gpu_load = 0
print("No GPUs detected.")
# Trigger energy-saving if CPU or GPU load is too high
if cpu_load > 90 or gpu_load > 90: # Adjust thresholds as needed
print("High system load detected! Initiating energy-saving protocol... 🚨")
save_energy()
# Set up the scheduler
scheduler = BlockingScheduler()
scheduler.add_job(check_system_load, 'interval', minutes=1) # Check every minute
How can we save the planet in other programming languages? Let's discuss!
jokes Article's
30 articles in total
Meme Tuesday 🚱
read article
Classic Programmer Jokes
read article
Meme Monday
read article
Um reels meu atingiu 1 milhão de visualizações com humor para desenvolvedores
read article
Joke Generator
read article
2024 dev.to roast 🔥
read article
JavaScript is the only language that understands programmers when they're feeling a little null.
read article
Meme Monday
read article
Programming Memes 🚀
read article
Plesk users keep discussing the price increase
read article
Country administration is a bad implementation of programming good practices
read article
Meme Monday
read article
If Computers Could Talk, What Would They Say About Us?
read article
Meme Monday
read article
Meme Monday
read article
The world without CSS
read article
Meme Monday
read article
Meme Monday
read article
jokes
read article
The Memes Of The Day
read article
The Memes Of The Day
read article
Meme Monday
read article
Programmers' workplaces always have a special vibe 😍
read article
The Memes of the day
read article
Meme Monday
read article
Why do JavaScript developers hate funerals? Because every time they hear "Your function has no body," they panic and start debugging💀💀
read article
Meme Monday
read article
Agile Body Snatchers
read article
Automate Saving the Planet... Or Just Your Computer's Energy 🐍
currently reading
Meme Monday
read article
Featured ones: