简体   繁体   English

如何每分钟运行 function 6 秒(Python)?

[英]How can I run a function for 6 seconds at every minute (Python)?

My program needs to get some data from an API, but at a constant interval and for just 6 seconds at every 60 seconds.我的程序需要从 API 获取一些数据,但间隔固定,每 60 秒仅 6 秒。 Running a while loop without stopping is too CPU intense, and I do not want that.不停地运行 while 循环会占用大量 CPU,我不希望这样。

while loop:
    candles = get_candles(ACTIVE, TIMEFRAME, 6, time()) 
    # get data while the loop is running
....

All I need is to stop the loop for 60 seconds and run the loop from second 57 to second 3 at every 60 seconds.我只需要停止循环 60 秒,然后每 60 秒运行一次从第 57 秒到第 3 秒的循环。 All the data I need is in this 6 seconds interval.我需要的所有数据都在这 6 秒的时间间隔内。

For example, from 14:00:57 to 14:01:03 I retrieve the data, and the rest of the time, the function waits.比如14:00:57到14:01:03我取数据,时间的rest,function等待。

Is that possible?那可能吗?

import time

start = time.time()
while loop:
    check = time.time()
    candles = get_candles(ACTIVE, TIMEFRAME, 6, time())
    if check - start >= 6:
        time.sleep(54)
        start = time.time()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM