python 定时执行python脚本
时间: 2023-12-05 14:41:54 浏览: 182
以下是两种Python实现定时执行脚本程序的方法:
1. 使用time模块的sleep()函数实现定时执行
```python
import time
# 定义需要执行的函数
def func():
print("Hello, world!")
# 定义定时执行函数
def timer(n):
while True:
# 获取当前时间
now = time.strftime('%H:%M:%S', time.localtime(time.time()))
# 判断当前时间是否为定时时间
if now == n:
# 执行函数
func()
# 等待一天
time.sleep(86400)
else:
# 每隔一秒检测一次
time.sleep(1)
# 设置定时时间
n = '10:00:00'
# 启动定时器
timer(n)
```
2. 使用APScheduler模块实现定时执行
```python
from apscheduler.schedulers.blocking import BlockingScheduler
# 定义需要执行的函数
def func():
print("Hello, world!")
# 创建定时器对象
scheduler = BlockingScheduler()
# 添加定时任务
scheduler.add_job(func, 'cron', hour=10)
# 启动定时器
scheduler.start()
```
阅读全文
相关推荐

















