【发布时间】:2013-07-07 18:01:06
【问题描述】:
正如您在下面看到的,这是我创建的脚本的副本,有人可以帮助我改进/修复我的 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 模块构建了一段示例代码,但它不起作用,我无法弄清楚如何让它工作,而且它也让我感到困惑。我还在学习,这很混乱。
【问题讨论】:
-
如果代码运行良好,最好将其发布到codereview.stackexchange.com
-
这些比较 (
currentime < '23:59:59') 是您真正想要做的吗? (只是想知道,也许date类可以适当地处理它) -
您可能想查看内置的event scheduler。所以你不必重新发明轮子......
-
这个问题似乎是题外话,因为它只是您只想查看的工作代码。试试Code Review。
-
另外,您真的想每天 24 小时每 1 秒消耗一次 CPU 时间(防止计算机进入睡眠/低功耗模式)吗?
标签: python date python-2.7 time while-loop