在python中以1小时为间隔运行一行

Ami*_* KK 1 python

我正在尝试完全在python中创建一个基于espeak的会话时钟

我想知道怎么跑

spk.speak('It is now <time>')
Run Code Online (Sandbox Code Playgroud)

每次系统点击时间如12:00 1:00等

Joh*_*ooy 10

import time
while True:
    # sleep for the remaining seconds until the next hour
    time.sleep(3600-time.time()%3600)
    spk.speak('It is now <time>')
Run Code Online (Sandbox Code Playgroud)


Bur*_*lid 6

您没有指定您正在使用的操作系统,但是在一定时间间隔运行是cronLinux和Unix上某些类似进程的工作.在Windows上,有Windows任务调度程序.

您可以设置这些系统以定期运行您的命令.

例如,此cron条目每小时运行一次脚本:

0 * * * * /usr/bin/python /home/myuser/somefile.py

你也使用:

@hourly /usr/bin/python /home/myuser/somefile.py

@hourly 是别名 0 * * * *