python - 如何安排函数在第一次在 python 中运行后每 3 天运行一次
问题描述
我有一个函数,我需要在 3 天后使用 python 运行它
def hello()
print("hello world")
脚本将运行,如何在python中每3天打印一次
解决方案
正如@Nuts 在评论中所说,cron
如果您想每三天运行一次整个程序,这是最好的选择。但是,如果您正在运行微服务或其他东西,并且只想每三天执行一个特定方法,则可以使用计时器。
import threading
my_timer = None
def make_thread():
# create timer to rerun this method in 3 days (in seconds)
global my_timer
my_timer = threading.Timer(259200, make_thread)
# call hello function
hello()
第一次只调用make_thread()
一次hello()
,然后它会每三天调用一次(很可能会有几秒钟的错误余量),只要程序保持运行。
推荐阅读
- laravel-5 - 如何运行工匠命令计划:在“hostgator 共享”托管服务器上运行?(Laravel 5.7)
- c# - x86 UWP DLL 的 DllNotFoundException
- java - 在单个 micronaut 项目中运行多个微服务的配置
- excel - 根据三个条件更新单元格值
- python-3.x - 根据日期聚合数据
- angular - Angular:无法访问组件内的数据
- amazon-web-services - cloudwatch 命令获取指标数据
- sql - 如何在 Oracle 连接中仅查询非系统数据库?
- javascript - 如何在两个不同的地方重用容器逻辑
- php - jQuery如何在选项菜单中附加以指定项目