gpt4 book ai didi

python定时执行代码

转载 作者:行者123 更新时间:2023-11-28 16:53:04 28 4
gpt4 key购买 nike

我想在每次执行时都在完全相同的时间执行我的一段代码,有点像播放媒体文件...(同一段代码每次都以完全相同的时间执行)

这在 python 中可能吗?

最佳答案

这应该可以解决问题:

def run_with_delay(funcs, interval):
for f in funcs[:-1]:
before = time()
f()
# compensate the interval with the execution time.
# NB: careful for functions that have a greater
# execution time than interval
after = time()
if after - before < interval:
sleep(interval - (after - before))
# last function is taken separately because we don't need
# an extra useless sleep
funcs[-1]()

关于python定时执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4701576/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com