在python脚本中的特定时间段内完成任务
正如您在下面看到的,这是我创建的脚本的副本,有人能帮我改进/修复我的在python脚本中的特定时间段内完成任务,python,date,python-2.7,time,while-loop,Python,Date,Python 2.7,Time,While Loop,正如您在下面看到的,这是我创建的脚本的副本,有人能帮我改进/修复我的currenttime任务吗 基本上,在白天,每隔3分钟+60秒定期呼叫测试,每个周期应执行以下任务: 在23:30:00到23:40:00之间的任何地方,它都会将clearscreen变为false 在23:40:00到23:50:00之间的任何时间,它检查clearscreen是否为false,如果为false,则清除某些文件,然后立即将clearscreen设置为false 在23:50:00到01:30:00之间的任何
currenttime
任务吗
基本上,在白天,每隔3分钟+60秒定期呼叫测试,每个周期应执行以下任务:
- 在23:30:00到23:40:00之间的任何地方,它都会将clearscreen变为false
- 在23:40:00到23:50:00之间的任何时间,它检查clearscreen是否为false,如果为false,则清除某些文件,然后立即将clearscreen设置为false
- 在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)
你提到我反复更新currenttime,然后打印,然后睡觉。我怎样才能避开这个“问题”
关于
我不确定您的意思;“你没有锁”全球透明屏幕
我正在运行Windows,因此signal.setitemer是不允许的,而且我需要在内存中为脚本存储某些值/变量,这样Windows上的计划任务就不合适了 您使用
模块构建了一段示例代码,但它不起作用,我无法确定如何让它起作用,而且对我来说也相当混乱。我仍在学习,这相当令人困惑。Python无法“忽略while循环”(除非在它们之外还有其他条件)。但是,如果while循环告诉Python循环0次,它将完全做到这一点sched
首先,你有这个循环:
这意味着while currenttime > '23:30:00' and currenttime < '23:40:00':
从来都不是实际的当前时间。通常是一秒钟前。想想这对边上的循环条件有什么影响 作为补充说明,currenttime
不能保证只睡一秒钟。如果您的计算机正忙,或者决定进入低功耗模式,它可能会持续一秒以上。如果中断在飞行,它可以短于一秒钟。即使在最好的情况下,它也会在一个方向或另一个方向上偏离半个时钟。所以,如果你需要它精确地发射600次,它通常不会这样做sleep(1)
同时,你得到了:
显然,在您当前的代码中,任何人都不可能改变这一点,所以在您的实际代码中,您可能试图从另一个线程中改变它。但是你没有锁。所以,很有可能你不会马上看到变化,甚至永远都不会global clearscreen
编写调度程序比看起来要困难得多。这就是为什么你通常最好使用现有的。选择包括:- stdlib模块
- stdlib
- stdlib。仅在具有真实信号的平台上(意味着不是Windows),如果您使用的是线程或
,则可能不适用于某些平台李>fork
- ActiveState上PyPI/recipes上的各种第三方模块为您提供了更好的
计时器(例如,使用单个计时器线程和即将到来的作业队列,而不是每个作业使用一个线程)
- 一个处理计时器的事件循环框架,如果您只需要调度,那么这可能会有点过头,如果您有其他原因使用Twisted、wx、PyGame或gevent,让它来调度
- 在任何Unix上运行脚本cron的外部计时器,在Mac和其他Unix上启动服务,在Windows上调度任务,等等。可能不适合每秒运行一个任务,但从您的评论来看,您真正需要的似乎是“能够每3分钟+-60秒调用一个函数”
由于您特别询问了
…有两种方法:sched
- 一次制定一整天的日程安排,并反复调用
,将当天的任务加上明天午夜运行的另一个任务,完成同样的任务enterabs
- 编写一个函数,根据当前时间计算出下一步计划的任务和时间,并执行此操作。在实际工作之后调用该函数 第一个是这样的:
import sched
import datetime
import time
s = sched.scheduler(time.time, time.sleep)
def dotoday():
now = datetime.date.now()
stime = now.time()
# Schedule "first" every 3 minutes from 22:00 to 22:57
if stime < datetime.time(22, 0):
stime = datetime.time(22, 0)
while stime <= datetime.time(22, 57):
s.enterabs(stime, 1, first, ())
stime += datetime.timedelta(0, 180)
# Schedule "second" every 3 minutes from 23:00 to 23:57
stime = datetime.time(23, 0)
while stime <= datetime.time(23, 57):
s.enterabs(stime, 1, second, ())
stime += datetime.timedelta(0, 180)
# Schedule "dotoday" to run tomorrow
midnight = now.replace(hour=0, minute=0, second=0)
tomorrow = midnight + datetime.timedelta(1, 0)
s.enterabs(tomorrow, 1, dotoday, ())
dotoday()
s.run()
导入计划
导入日期时间
导入时间
s=sched.scheduler(time.time,time.sleep)
def dotoday():
now=datetime.date.now()
stime=now.time()
#从22:00到22:57,每3分钟安排一次“第一次”
如果时间 如果代码运行良好,那么最好将其发布在上面。这些比较(currentime<'23:59:59'
)是您真正想要做的吗?(只是想知道,date
类是否适当地处理了它)您可能想查看内置的。因此,您不必重新发明轮子……这个问题似乎离题了,因为它只是工作代码,您只需要查看它。试试看。另外,你真的想每1秒(防止计算机进入睡眠/低功耗模式)24小时/天消耗CPU时间吗?我更新了我的原始帖子,对你的评论进行了编辑,感谢您迄今为止的帮助,非常感谢。对于您对这个答案的具体评论:sched
代码只是一个概念证明;关键是你要理解它,而不是在不知道它意味着什么的情况下运行它。你说这让你困惑…什么部分让你困惑?你看过链接的文档了吗?您不知道datetime对象是如何工作的,您不知道什么是enter
和enterabs
做的,还是其他什么?
currenttime = strftime('%H:%M:%S')
print ("""23:40:00 to 23:50:00 | %s""" % (currenttime))
sleep(1)
global clearscreen
import sched
import datetime
import time
s = sched.scheduler(time.time, time.sleep)
def dotoday():
now = datetime.date.now()
stime = now.time()
# Schedule "first" every 3 minutes from 22:00 to 22:57
if stime < datetime.time(22, 0):
stime = datetime.time(22, 0)
while stime <= datetime.time(22, 57):
s.enterabs(stime, 1, first, ())
stime += datetime.timedelta(0, 180)
# Schedule "second" every 3 minutes from 23:00 to 23:57
stime = datetime.time(23, 0)
while stime <= datetime.time(23, 57):
s.enterabs(stime, 1, second, ())
stime += datetime.timedelta(0, 180)
# Schedule "dotoday" to run tomorrow
midnight = now.replace(hour=0, minute=0, second=0)
tomorrow = midnight + datetime.timedelta(1, 0)
s.enterabs(tomorrow, 1, dotoday, ())
dotoday()
s.run()