我需要从特定时间开始每隔一定时间间隔执行一个函数。
例如,如果开始时间是00:00:00,间隔是5秒,那么我需要这个函数的执行时间是:
00:00:00
00:00:05
00:00:10
00:00:15
...
23:59:55
正如你所看到的,我的观点是系统的时钟
在 stackoverflow 中存在许多与此问题相关的问题,例如 How to execute a function asynchronously every 60 seconds in Python?和 What is the best way to repeatedly execute a function every x seconds in Python? .此外,我看到其他解决方案 schedule 0.3.2 .
我的问题是,例如使用以下代码:
import schedule
import time
def job():
print("I'm working...")
print 'execute f time: ', time.ctime(time.time())
print 'execute f time: ', time.time()
schedule.every().minutes.do(job)
while 1:
schedule.run_pending()
time.sleep(1)
在这种情况下,当我运行代码时,代码的输出是:
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
使用模块计划我有同样的问题
请大家多多指教 提前致谢。
更新
好吧,我测试了 crontab,但不满意。首先我在 Ubuntu 中测试了我的用户的配置 crontab 文件,但后来我找到了一个库来向他的 crontab 文件配置写入指令,所以我使用库 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? ),而且我不知道为什么当我执行时每一分钟都是每一分钟分钟与一秒,我们的计算总和误差太大,但误差比上面的例子更合适
date.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 函数内的时间an 也为 0 秒。
最佳答案
我相信你最好依靠操作系统来进行调度而不是Python。使用具有单一功能的简单脚本,然后使用 crontab(如果使用 Linux)或 Windows 任务计划程序来安排执行会更简单。
更喜欢操作系统方法的原因之一是稳健性。例如,如果您依靠 Python 进行调度,那么如果发生未处理的异常,您的脚本将终止,直到再次手动执行。系统重启等也是如此。
如果您使用操作系统进行调度,即使在系统重新启动和未处理的异常的情况下,脚本也会继续按照预定义的时间表执行。
关于python - 每隔一定时间执行一个函数,不计算函数执行的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38346633/