Python 时间表 运行 不一致

Python schedule run with inconsistence

我有一个带有一些代码的主函数,我每隔预定时间就需要 运行 这个函数,但与我每 2-3 分钟配置它的时间无关 运行。我不知道发生了什么。我在下面展示了一些例子。

import schedule

def main():
    print('Some code here...')
    schedule.run_pending()

# the function main should be run every 30min...?
schedule.every(30).minutes.do(main)
schedule.every().hour.do(main)

main()

根据我的研究,这段代码应该 运行 每 30 分钟,但它 运行 每 2-3 分钟。

你不应该直接调用你预定的函数。在您想要的场景中,该函数应该 运行 每 X 分钟 - 这意味着负责 运行 设置它的脚本应该一直 运行 ,决定何时调用该函数。 while True 应该可以。

import schedule

def main():
    print('Some code here...')

# the function main should be run every 30min...?
schedule.every(30).minutes.do(main)
schedule.every().hour.do(main)

while True:
    schedule.run_pending()