Jump to content

[PYTHON] Safe way to shutdown script (that I compiled into an .exe file)

Wictorian

So I have python script that controls brightness based on time, and if it is past 12 I just want to set it to 100% and shut the program down. ( I compiled it into an .exe file) I have a while loop and and if statement that checks the time, and if it is greater than 12, it just breaks the loop, but I think apparently it causes some issues because as soon as the clock hits 12, a message pops up saying 'Failed to execute script ScreenAdjust'. Is there a way I can solve this? I just don't want the box to pop up everytime...

 

The script just in case:
 

Spoiler

import screen_brightness_control as sbc
import datetime
import time
a = str(datetime.datetime.now().time())
hour = (a[0]+a[1])
while(True):
	if (int(hour) => 8 and int(hour) < 12):
        	a = str(datetime.datetime.now().time())
        	hour = a[0]+a[1]        
        	sbc.set_brightness(int(hour)*4)
        	time.sleep(60)
	else:
		sbc.set_brightness(100)
		break

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

You should exit the program cleanly with sys.exit().

import sys

"""whatever"""

sys.exit()

I'm not sure how you "compiled" it though so maybe whatever you used is causing an issue that is independent of this.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

i generally do not like while true for alarms, seem like waste of cpu cycle to constantly be checking every millionth of a second. Can't you simply calculate the time for your program to wait and then put it to sleep in the meanwhile? 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, wasab said:

i generally do not like while true for alarms, seem like waste of cpu cycle to constantly be checking every millionth of a second. Can't you simply calculate the time for your program to wait and then put it to sleep in the meanwhile? 

As he said. You want to use a callback trigger at a certain time. Example on windows you are looking for "Windows Scheduler". When you give it a time it will interrupt "around" the time you requested it and your process will gain full CPU cycle again and get it's callback executed. If you do not do that you will pin your CPU to 100% usage on one of it's core (unless you multithread it where it would be all cores)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
On 4/16/2021 at 2:06 AM, wasab said:

i generally do not like while true for alarms, seem like waste of cpu cycle to constantly be checking every millionth of a second. Can't you simply calculate the time for your program to wait and then put it to sleep in the meanwhile? 

I check it every minute. How can you update the time if you dont use it?

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Wictorian said:

I check it every minute. How can you update the time if you dont use it?

1) find time until 12. Eg. If it is 5:30 atm then that time is 6 hours and 30 minutes. 

 

2) tell program to sleep for that many hours, e.g. 6 hours and 30 minutes. 

 

3) once awoken, turn down/up the brightness. 

 

Use something like sleep(), that way it won't constantly be running in a while loop.

 

 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×