我需要在某个特定的时间间隔内执行一个函数。在
例如,如果开始时间为00:00:00,间隔为5秒,则需要执行此函数的时间为:00:00:00
00:00:05
00:00:10
00:00:15
...
23:59:55
正如你所看到的,我的观点是系统的时钟
我的问题是,例如使用以下代码:
^{pr2}$
在这种情况下,当我运行代码时,代码的输出是:I'm working...
execute f time: Wed Jul 13 04:32:00 2016
execute f time: 1468398720.88
I'm working...
execute f time: Wed Jul 13 04:33:00 2016
execute f time: 1468398780.94
I'm working...
execute f time: Wed Jul 13 04:34:01 2016
execute f time: 1468398841.0
如您所见,这段代码每分钟执行一次,但是间隔从1分钟变为1分钟每1秒,这个错误对我很重要,可能是函数打印时间的原因。我需要继续每分钟执行一次,在这种情况下,没有由函数print的累计和引起的秒数
我的计时器解决方案也有相同的错误:import threading
import time
def f():
# call f() again in 10 seconds
threading.Timer(10, f).start()
# do something here ...
print 'execute f time: ', time.time()
time.sleep(5)
# start calling f now and every 10 sec thereafter
print 'init time: ', time.time()
f()
输出的一个片段显示,某个时刻的计算错误导致间隔大于5([0,5,10,16,21,26,….]),这对我来说是错误的,因为函数的执行时间对我很重要:init time: 1468407868.6
execute f time: 1468407868.6
execute f time: 1468407878.6
execute f time: 1468407888.6
execute f time: 1468407898.6
execute f time: 1468407908.61 # change the interval
execute f time: 1468407918.61
execute f time: 1468407928.61
对于sched模块,我也有同样的问题
有什么建议吗
提前谢谢。在
更新
好吧,我测试crontab,但我不满意。首先,我在Ubuntu中对用户的配置crontab文件进行测试,但是后来我发现了一个库来编写crontab文件配置的指令,所以我使用library Crontab#!/usr/bin/python
# -*- coding: utf-8 -*-
from crontab import CronTab
task = CronTab(user='cyberguille')
command='/home/cyberguille/date.py'
cron_job=task.new(command)
cron_job.setall('*/1, *, *, *, *')
task.write()
print task.render()
正如您可以看到的每一分钟执行代码,但我看到两个问题,在不到一分钟内执行是不容易的(参见How to run scripts every 5 seconds using cron?),我不知道为什么当我每一分钟执行一次是每一分钟一秒,我们有太复杂的和错误,但错误比上面的例子更适合
代码日期.py是这个吗#!/usr/bin/python
# -*- coding: utf-8 -*-
#import time
from datetime import datetime
tiempo = datetime.now()
#tiempo = time.strftime("%H:%M:%S")
archivo=open('/home/cyberguille/archivo_fecha.txt', 'a+')
archivo.write(str(tiempo) + '\n')
输出的一个片段是2016-07-19 00:11:01.307779
2016-07-19 00:12:01.365995
2016-07-19 00:13:01.421373
2016-07-19 00:14:01.477752
2016-07-19 00:15:01.532234
2016-07-19 00:16:01.588818
2016-07-19 00:17:01.649581
2016-07-19 00:18:01.705975
2016-07-19 00:19:01.759986
2016-07-19 00:20:01.816754
2016-07-19 00:21:01.873748
2016-07-19 00:22:01.927750
2016-07-19 00:23:01.980666
2016-07-19 00:24:02.036164
2016-07-19 00:25:01.090912
2016-07-19 00:26:01.148098
真的每一分钟你都可以说这不是重要的错误,但为什么不能执行,例如在00秒2016-07-19 00:11:00.307779
2016-07-19 00:12:00.365995
2016-07-19 00:13:00.421373
2016-07-19 00:14:00.477752
也许脚本的调用很慢。在
我找到了一个很好的解决方案这是脚本from multiprocessing import Process
from datetime import datetime
import time
import sched
def timer(interval):
sc = sched.scheduler(time.time, time.sleep)
sc.enter(1, 1, do_something, (sc,interval))
sc.run()
def do_something(sc,interval):
dateTime = datetime.now()
if(dateTime.second%interval==0):
p = Process(target=SensorContainer.run,args=(dateTime,))
p.start()
sc.enter(1, 1, SensorContainer.do_something, (sc,interval,))
def run(datetime):
print datetime
在这个解决方案中,计时器每分钟在0秒内工作一次,正如您可以看到的,我的度量是当我调用函数时,这与crontab中的不同,但是在任何情况下,我测试run函数内的时间也是0秒。在