我有一个 python 脚本(用它做一些事情selenium并将结果发送到我的 what'sapp [Used Twilio API])并且我将它托管在Heroku并计划运行one time per day。它按预期在预定时间运行,但也在其他时间运行(每天超过 6 次)。我不想在预定时间以外运行它。我怎样才能让它只在预定的时间运行?Procfile:
web: python my_script.py除此之外,我requirements.txt & my_script.py在我的目录中。
2 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
您可以使用time模块和while循环来检查当前时间是否等于您想要的时间。
from time import gmtime, strftime
desired_time = '13:28'
while True:
if strftime("%H:%M", gmtime()) == desired_time:
main()
杨__羊羊
TA贡献1943条经验 获得超7个赞
提供的答案可能是最简单的可能选项。这是我在个人项目中使用的。
您可以使用APScheduler来安排任务以一定间隔运行。
from apscheduler.schedulers.background import BackgroundScheduler
def start():
scheduler = BackgroundScheduler()
scheduler.add_job(my_logger, 'interval', seconds=3)
scheduler.start()
添加回答
举报
0/150
提交
取消
