首页 文章

仅使用1个web dyno和0个工作dynos运行Heroku后台任务

提问于
浏览
21

我在Heroku上有一个Python Flask应用程序,用于提供网页,但也允许启动某些任务,我相信这些任务最适合作为后台任务 . 因此,我按照Heroku rq tutorial设置了后台任务 . 我的Procfile看起来像这样:

web: python app.py
worker: python worker.py

但是,我的流程目前已缩放 web=1 worker=0 . 鉴于这个背景过程对我来说似乎是明智的,我可以为它配置一个完整的dyno,然后为那么小的东西支付34美元/月 .

Question:

  • 如果我在我的Procfile中声明了 worker 进程,但是将缩放保持在 web=1 worker=0 ,我的排队进程最终是否会在我的可用web dyno上运行?或者排队的进程永远不会运行?

  • 如果排队的进程永远不会运行,还有另一种方法可以做到这一点,例如,在我的Web应用程序中使用 twisted 来异步运行任务吗?

Additional Information

worker.py 看起来像这样:

import os
import redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

排队进程的主应用程序中的逻辑如下所示:

from rq import Queue
from worker import conn
q = Queue(connection=conn)

q.enqueue(myfunction, myargument)

4 回答

  • 3
    $ cat Procfile
    web: bin/web
    
    $ cat bin/web
    python app.py &
    python worker.py
    
  • 11

    我目前只使用1个dyno在Heroku中运行我的web和后端调度程序 .

    想法是为Heroku提供一个主要的python脚本,以1 dyno开始 . 此脚本用于启动Web服务器进程和客户调度程序进程 . 然后,您可以定义作业并将其添加到自定义计划程序 .

    在我的案例中使用APScheduler .

    这就是我做的:

    在Procfile中:

    web: python run_app.py    #the main startup script
    

    在run_app.py中:

    # All the required imports
    from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
    from apscheduler.triggers.cron import CronTrigger
    from run_housekeeping import run_housekeeping
    from apscheduler.schedulers.background import BackgroundScheduler
    import os
    
    def run_web_script():
        # start the gunicorn server with custom configuration
        # You can also using app.run() if you want to use the flask built-in server -- be careful about the port
        os.system('gunicorn -c gunicorn.conf.py web.jobboard:app --debug')  
    
    def start_scheduler():
    
         # define a background schedule 
         # Attention: you cannot use a blocking scheduler here as that will block the script from proceeding.
         scheduler = BackgroundScheduler()
    
         # define your job trigger
         hourse_keeping_trigger = CronTrigger(hour='12', minute='30')
    
         # add your job
         scheduler.add_job(func=run_housekeeping, trigger=hourse_keeping_trigger)
    
         # start the scheduler
         scheduler.start()
    
    
    def run():
        start_scheduler()
        run_web_script()
    
    if __name__ == '__main__':
        run()
    

    我也使用了4个工作进程来为Gunicorn提供网络服务 - 运行得非常好 .

    在gunicorn.conf.py中:

    loglevel = 'info'
    errorlog = '-'
    accesslog = '-'
    workers = 4
    

    您可能想要签出此项目作为示例:Zjobs@Github

  • -2

    您可以使用流程管理器,例如godmonit .

    有了上帝,你可以像这样设置你的配置

    God.watch do |w|
      w.name = "app"
      w.start = "python app.py"
      w.keepalive
    end
    
    God.watch do |w|
      w.name = "worker"
      w.start = "python worker.py"
      w.keepalive
    end
    

    然后你把它放在你的Procfile中

    god -c path/to/config.god -D
    

    默认情况下,如果进程崩溃,它会自动重新启动进程,如果内存使用率过高,您可以将其配置为重新启动应用程序 . 查看文档 .

  • 2

    您应该看一下Heroku Scheduler它将允许您按计划的时间间隔(例如每10分钟)运行一个特定的任务 . 如果您已经有工作人员设置,则可以添加:

    heroku run worker
    

相关问题