import threading
import os
def shutdown():
os.system("shutdown -s")
# user setting zone!!!
hour = 0
minute = 20
sec = 0
# user setting zone!!!
total_sec = hour*3600.0 + minute*60.0 + sec - 60.0
if total_sec < 0:
total_sec = 0
print("The computer will be Shut Down in (%d hour, %d minute, %d second).\n" %(hour, minute, sec))
if total_sec >= 120:
temp_sec = total_sec - 120
threading.Timer(temp_sec, lambda: print("Last 3 minutes before shuting down the computer!!\n")).start()
else:
print("Less than 3 minutes before shuting down the computer!!\n")
threading.Timer(total_sec, shutdown).start()
The code is shown above. When I set a short time like 10 min, 20 min or a little longer, the script could work normally. But if I set the waiting time to a long time like 4 hours or 5 hours, the script could NOT work at all. Nothing would happen when the time is up. Could you pls point out why the error happens and guide me to fix it? Thanks in advance.
Have you actually timed it? You say it works normally, but does it actually shut the computer off in 10 minutes when set for 10 minutes, and not say 15+ minutes?
I ask because it looks like you have it set to give you a 3 minute warning. However then the timer resets because you use total_sec in "threading.Timer(total_sec, shutdown).start()" So when you set it for say 60 minutes, it gives you a warning at 57 minutes, then runs for another 60 minutes.
Therefore, I suspect if you let it run for 11 hours, when you set it for 5 hours, it would actually shutoff the computer.
Related
I'm trying to run a function approximately 200 times a second (give or take +- 50). When using time.sleep(0.005), I find that it sleeps for much longer. I understand that time.sleep is not accurate, but my use case doesn't need much accuracy either.
I searched this problem up and it seems to be an issue with Windows 10 & 11 in particular: https://bugs.python.org/issue44681
They don't seem to be keen on fixing this issue, so I was wondering what alternatives are there for sleeping for around a millisecond?
I tested this code:
last_time = time.time()
counter = 0
while True:
now = time.time()
if now - last_time >= 1:
print(counter)
last_time = now
counter = 0
counter += 1
Which gave me around 64 to 65 on my computer and around 70 on my laptop.
I have a time - "09:20:45"
And I want to schedule my code after every 30 minutes of this time - "09:20:45".
I don't know how can I do this?
schedule.every(30).minutes.do(output_file_exporting)
while True:
schedule.run_pending()
time.sleep(1)
where output_file_exporting is my function name.
So I want to run my code after 09:20:45 on the increment of every 30 minutes
like -
09:50:45
10:20:45
10:50:45
11:20:45
11:50:45
on these time my code should be run.
You're making this harder than it needs to be. You just do it like you describe it.
import time
import datetime
while True:
now = datetime.datetime.now()
if now.hour >= 9 and now.minute in (20,50):
break
time.sleep(60)
output_file_exporting()
schedule.every(30).minutes.do(output_file_exporting)
...
I would like to schedule a task at minute :57 and minute :05, alternating every two hours. So the intervall during execution is alternating between 52 minutes and 68 minutes, on average 60 minutes.
For a given hour x the times when I would like to execute code are:
x:05; x:57; (x+2):05, (x+2):57; ...
I currently use the python module schedule and the following code:
schedule.every(2).hours.at(':05').do(job1)
schedule.every(2).hours.at(':57').do(job2)
while True:
schedule.run_pending()
time.sleep(1)
How can I best ensure that the code is not running at x:57 and then again at (x+1):05 the following hour (meaning the intervall is only 8 minutes long)?
Edit: I could add something along the lines of:
minutes = datetime.now().minutes
if not (minutes < 5 or minutes > 57):
time.sleep(60 - minutes)
and then schedule but I would miss 2 executions which is not ideal.
I'm creating a test for Google Assistant based on while loop. The code will play a long mp3 file and I'm try to identify if the assistant will do a false trigger and count how many times.
I'm running on pycharm/pytest and getting the trigger status by UIAutomator provided by Google.
import android
import time
play_music(music.mp3)
start_time = time.time()
trigger = 0
hours = 1
command_timeout = hours * 60 * 60
while trigger < 3 or time.time() - start_time < command_timeout:
if trigger_screen.is_in_screen():
trigger += 1
time.sleep(10)
stop_music()
The conditions to stop the loop is 3 false triggers or one hour of test, but the loop isn't stop after one hour of test, could someone help me?
You're using an or statement when you should be using an and statement:
while trigger < 3 and time.time() - start_time < command_timeout:
With your current code, the while loop only terminates when BOTH conditions are
False, when you really want it to terminate when either one or the other is False.
You need to swap the or in your code with and like shown below:
import android
import time
play_music(music.mp3)
start_time = time.time()
trigger = 0
hours = 1
command_timeout = hours * 60 * 60
while trigger < 3 and time.time() - start_time < command_timeout:
if trigger_screen.is_in_screen():
trigger += 1
time.sleep(10)
stop_music()
Bassically the code you wrote continues the loop as long as one of the conditions is met, which explains why your music continued to play (less than 3 triggers so the loop still runs)
I'm trying to make a break timer. I can get the current second value, but I don't know how to reset the seconds and start counting down
I've tried several formulas found here on stack overflow, but have yet to find what I'm looking for
import time
while True:
now = time.localtime(time.time())
print(now[5])
time.sleep(1)
I expect the output to count down from 59 and start over
output: count up from current second
Why don't you use something like:
import time
sec = 0
while True:
print(59 - sec)
time.sleep(1)
sec = (sec + 1) % 60
Here is version with a defined function. It will countdown at defined seconds, taking sleep every seconds.
import time
def countdown(t_sec):
while t_sec:
mins, secs = divmod(t_sec, 60)
timeformat = '{:02d}'.format(secs)
print(timeformat)
time.sleep(1)
t_sec -= 1
countdown(59)