您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Python中使用Flask执行定期任务

如何在Python中使用Flask执行定期任务

您可以将cron用于简单任务。

为您的任务创建一个烧瓶视图。

# a separate view for periodic task
@app.route('/task')
def task():
    board.read()
    board.digital_outputs = board.digital_inputs

然后使用cron,定期从该网址下载

# cron task to run each minute
0-59 * * * * run_task.sh

run_task.sh的内容在哪里

wget http://localhost/task

Cron无法每分钟运行一次以上。如果需要更高的频率(例如,每5秒=每分钟12次),则必须按以下方式在tun_task.sh中进行操作

# loop 12 times with a delay
for i in 1 2 3 4 5 6 7 8 9 10 11 12
do
    # download url in background for not to affect delay interval much
    wget -b http://localhost/task
    sleep 5s
done
Python 2022/1/1 18:52:02 有519人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶