简体   繁体   English

如何获取y和z之间的每x分钟循环的python代码?

[英]How do I get python code to loop every x mins between the times y and z?

This might be incredibly easy but how do I get python code to loop every x mins between the times y and z? 这可能非常简单但是如何让python代码在y和z之间循环每x分钟?

For example if I wanted my script to run between midnight (00:00) through to 10 pm (22:00) looping every 5 minutes. 例如,如果我希望我的脚本在午夜(00:00)到晚上10点(22:00)之间运行,每5分钟循环一次。

Try the sched module in the standard library. 尝试使用标准库中的sched 模块 Here's an example of calling a function once per second, starting five seconds in the future, and ending ten seconds in the future: 下面是一个示例,每秒调用一次函数,从将来的五秒开始,到将来的十秒结束:

from sched import scheduler
from time import time, sleep

s = scheduler(time, sleep)

def run_periodically(start, end, interval, func):
    event_time = start
    while event_time < end:
        s.enterabs(event_time, 0, func, ())
        event_time += interval
    s.run()

if __name__ == '__main__':

    def say_hello():
        print 'hello'    

    run_periodically(time()+5, time()+10, 1, say_hello)

Alternatively, you can work with threading.Timer , but you need to do a little more work to get it to start at a given time, run every five minutes, and stop at a fixed time. 或者,您可以使用threading.Timer ,但是您需要做一些工作才能让它在给定时间启动,每五分钟运行一次,并在固定时间停止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ply 文件中获取 python 中给定点 ( x, y ) 的 z 轴坐标? - How do I get z-axis coordinate from ply file for a given point ( x, y ) in python? 如何在Python的单行代码中使用for循环打印随机样本(带有random.sample(range(x,y),z))? - How can I print a random sample (with random.sample(range(x,y), z)) using a for-loop in a SINGLE line of code in Python? 如何在Python中获取每次单击的3D模型的(x,y,z)坐标? - How to get the (x,y,z) coordinates of a 3D model of every click in Python? python - 从x小时y分钟格式将时间转换为z分钟 - python - Convert time into z mins from x hours y mins format 我试图在两次之间循环,每隔15分钟从8:00到17:00 - I am trying to loop between two times, from 8:00 to 17:00 for every 15 mins 如何在 python 中获得 function 以在 2 个列表(每个列表都有自己的 x、y)之间进行插值? - How do I get a function in python to interpolate between 2 list (each with its own x, y)? 如何将y = pow(x,z)求反得到x? - How can I invert y = pow(x, z) to get x? 我想做一个循环,对于每个 x 动作做 y - i want to make a loop, for every x actions do y Python中用“x或y中的x:”做什么? - What does “for x in y or z:” do in Python? 如何从三个数组创建x,y,z坐标,其中x和y是使用meshgrid生成的,而z取决于x? - How do I create x,y,z coordinates from three arrays, where x and y are generated with meshgrid and z is dependent on x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM