As you can see below here is a copy of my script what I've created, could someone help me improve/fix my currenttime tasks.
Basically, during the day periodically calls test every 3 mins +- 60 seconds each periodically cycle it is supposed to do the follow tasks:
- Anywhere during 23:30:00 to 23:40:00 it turns clearscreen to false
- Anywhere during 23:40:00 to 23:50:00 it checks if clearscreen is false and if it is then it clears certain files and then it immediately sets clearscreen to false
Anywhere during 23:50:00 to 01:30:00 it just idles, doing nothing other than checking the time.
from datetime import date, timedelta from sched import scheduler from time import time, sleep, strftime import random clearscreen = 0 def periodically(runtime, intsmall, intlarge, function): global clearscreen ## Get current time currenttime = strftime('%H:%M:%S') ## while currenttime is anywhere between 23:40 and 23:50 then... while currenttime > '23:30:00' and currenttime < '23:40:00': ## Update time currenttime = strftime('%H:%M:%S') print ("""23:30:00 to 23:40:00 | %s""" % (currenttime)) sleep(1) ## If clearscreen = false then... if clearscreen == False: ## Set clearscreen to true to enable the next part work and to disable this part until the next day. clearscreen = True print "changed to true" ## If currenttime is anywhere between 23:50 and 23:59 then... while currenttime > '23:40:00' and currenttime < '23:50:00': ## Update time currenttime = strftime('%H:%M:%S') print ("""23:40:00 to 23:50:00 | %s""" % (currenttime)) sleep(1) if clearscreen == True: ## clear stuff here print "cleared" ## Change clearscreen to to stop it running clearscreen = False print "changed to false" ## Only allow run_periodically during 01:30 and 23:30 if clearscreen == False: while currenttime > '23:50:00' and currenttime < '23:59:59': ## Update time currenttime = strftime('%H:%M:%S') print ("""23:50:00 to 23:59:59 | %s""" % (currenttime)) sleep(1) while currenttime > '00:00:00' and currenttime < '01:30:00': ## Update time currenttime = strftime('%H:%M:%S') print ("""00:00:00 to 01:30:00 | %s""" % (currenttime)) sleep(1) runtime += random.randrange(intsmall, intlarge) s.enter(runtime, 1, function, ()) s.run() def test(): print "test function" while True: periodically(180, -60, +60, test)
Any help would be much appreciated.
Edit for @abarnert
In regards to the loops this is what the function is supposed to be doing:
23:3x:xx - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 23:30:00 and 23:40:00)
23:3x:xx - clearscreen is false, setting it to true.
23:3x:xx to 23:40:00 - currenttime is updated every 1 second(s)
23:40:01 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 23:30:00 and 23:40:00)
23:40:01 - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 23:40:00 and 23:50:00)
23:40:01 - clearscreen is true, doing some stuff and then changing clearscreen to false
23:40:01 to 23:50:00 - currenttime is updated every 1 second(s)
23:50:01 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 23:40:00 and 23:50:00)
23:50:01 - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 23:50:00 and 23:59:59)
23:50:01 to 23:59:59 - currenttime is updated every 1 second(s)
00:00:00 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 23:50:00 and 23:59:59)
00:00:00 - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 00:00:00 and 01:30:00)
00:00:00 and 01:30:00 - currenttime is updated every 1 second(s)
00:00:00 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 00:00:00 and 01:30:00)
You mention that I repeatedly update currenttime, then print, then sleep. How would I get around this "problem"?
In regards to the global clearscreen I am unsure what you mean by; "you don't have a lock on it"
I'm running Windows so signal.setitemer is a no go and also I need certain values/variables to be stored in memory for my script so scheduled tasks on windows won't be appropriate correct?
You built an example bit of code using the sched module but it doesn't work and I am unable to work out how to get it working, plus it's also rather confusing for me. I'm still learning and it's rather confusing.
currentime < '23:59:59') what you really want to do? (Just wondering, maybe thedateclass handles it appropriately)