简体   繁体   English

Flask 部署在 heroku 上的 App Apscheduler 无法在后台运行

[英]Flask App Apscheduler not working in background deployed on heroku

In my flask app I want to schedule a function to run in a regular time interval which I have deployed on heroku .在我的 flask 应用程序中,我想安排一个 function 以我在heroku上部署的固定时间间隔运行。 Which works perfectly when the interval is in few minutes or seconds but not for hours.当间隔是几分钟或几秒钟而不是几个小时时,它可以完美地工作。 I am using Aps Scheduler.我正在使用 Aps 调度程序。

Here is my python file which contains the scheduler.这是我的 python 文件,其中包含调度程序。

#price_drop.py

from apscheduler.schedulers.background import BackgroundScheduler
schedule = BackgroundScheduler()

def CheckPrice():
    print("Running")

schedule.add_job(id= "Tracker",func = CheckPrice,trigger='interval',hours = 1)
schedule.start()

There are other features which should run in parallel(routes).还有其他功能应该并行运行(路由)。

run.py运行.py

from app import app

if __name__ == '__main__':
   app.run(debug=True)

Here are some heroku configurations下面是heroku的一些配置

Procfile简介

web: gunicorn app:app
clock: python price_drop.py

Also Added below command in powershell还在 powershell 中添加了以下命令

heroku ps:scale clock=1 heroku ps:比例时钟=1

I think the scheduler is running in background so I need to keeping the file alive.我认为调度程序在后台运行,所以我需要让文件保持活动状态。 Can plz anyone suggest me an efficient way to do this.请任何人都可以建议我一个有效的方法来做到这一点。 Or am I going wrong anywhere in the code.或者我在代码的任何地方都出错了。

If you are trying to run actual computation within the clock process.如果您尝试在时钟进程中运行实际计算。 It is not the way to handle scheduled executions in heroku.这不是处理 heroku 中计划执行的方式。

You'll need你需要

  • A single clock process (like the one you already have).一个时钟进程(就像你已经拥有的那个)。 Which will queue tasks in a message queue like redis它将在消息队列中排队任务,如 redis
  • A worker process that listens to redis queue and processes tasks监听redis队列并处理任务的worker进程
  • A message queue like redis消息队列如 redis

Here some guides about achieving what you want这里有一些关于实现你想要的东西的指南

If you are not planning on scaling up the web worker beyond 1, you can directly add APScheduler within the Flask app.如果您不打算将 web worker 扩展到 1 以上,您可以直接在 Flask 应用程序中添加 APScheduler。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM