正如您在下面看到的,这是我创建的脚本的副本,有人可以帮助我改进/修复我的currenttime
任务。
基本上,白天每隔 3 分钟 +- 60 秒定期调用一次测试,每个周期循环它应该执行以下任务:
- 在 23:30:00 到 23:40:00 期间的任何地方,它会将 clearscreen 变为 false
- 在 23:40:00 到 23:50:00 期间的任何地方,它都会检查 clearscreen 是否为假,如果是,则清除某些文件,然后立即将 clearscreen 设置为假
在 23:50:00 到 01:30:00 期间的任何地方,它都会闲置,除了检查时间之外什么都不做。
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)
任何帮助将非常感激。
编辑@abarnert
关于循环,这是函数应该做的事情:
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)
你提到我反复更新当前时间,然后打印,然后睡觉。我将如何解决这个“问题”?
关于global clearscreen
我不确定你的意思;“你没有锁”
我正在运行 Windows,所以 signal.setitemer 是不行的,而且我需要将某些值/变量存储在我的脚本的内存中,所以 Windows 上的计划任务不合适吗?
您使用该模块构建了一个示例代码,sched
但它不起作用,我无法弄清楚如何让它工作,而且它对我来说也相当混乱。我还在学习,这很混乱。