python 函数执行时间间隔_每隔一定时间执行一个函数,而不计算该函数的执行时间...

我需要在某个特定的时间间隔内执行一个函数。在

例如,如果开始时间为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秒。在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,有多种方法可以计算函数执行时间。其中一种简洁的方式是使用时间戳进行计算。你可以使用time模块中的time()函数获取当前时间戳,然后在函数执行前后分别获取时间戳,并计算它们的差值来得到函数执行时间。下面是一个示例代码: ```python import time def func(): print('func start') time.sleep(1) print('func end') t = time.time() func() print(f'coast:{time.time() - t:.4f}s') ``` 在这个示例中,我们首先使用time.time()获取当前时间戳t,然后调用func()函数函数中包含了一钟的延迟。最后,我们再次使用time.time()获取当前时间戳,并计算它们的差值,得到函数执行时间。输出结果会显示函数执行时间。 除了使用时间戳,还有其他几种方法可以计算函数执行时间。比如使用time模块中的perf_counter()函数、使用timeit模块中的timeit()函数、使用装饰器统计运行耗时以及使用with语句统计运行耗时等方法。你可以根据具体的需求选择适合的方法来计算函数执行时间。 #### 引用[.reference_title] - *1* *2* [Python计算函数执行时间(五种案例)](https://blog.csdn.net/weixin_38924500/article/details/111679503)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [几种Python执行时间计算方法](https://blog.csdn.net/wangshuang1631/article/details/54286551)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值