仅使用 1 个 web dyno 和 0 个 worker dyno 运行 Heroku 后台任务 [英] Running Heroku background tasks with only 1 web dyno and 0 worker dynos

查看:24
本文介绍了仅使用 1 个 web dyno 和 0 个 worker dyno 运行 Heroku 后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Heroku 上有一个 Python Flask 应用程序,它为网页提供服务,但也允许启动某些任务,我认为最好将其构建为后台任务.因此,我遵循了 Heroku rq 教程设置后台任务.我的 Procfile 看起来像这样:

I have a Python Flask app on Heroku that serves web pages but also allows certain tasks to be launched which I believe would be best structured as background tasks. As such, I've followed the Heroku rq tutorial to set up background tasks. My Procfile looks like this:

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

但是,我的流程目前是按比例缩放的web=1 worker=0.鉴于此后台进程不会经常运行,我认为为它提供整个 dyno 然后每月支付 34 美元购买这么小的东西似乎并不明智.

However, my processes are currently scaled web=1 worker=0. Given that this background process won't be run very often, it doesn't seem sensible to me to provision an entire dyno for it and then pay the $34/month for something that small.

问题:

  • 如果我保留在 Procfile 中声明的 worker 进程,但将缩放保持在 web=1 worker=0,我排队的进程最终会在我的可用网络上运行吗?动力?或者排队的进程永远不会运行?
  • 如果排队的进程永远不会运行,是否还有其他方法可以做到这一点,例如,在我的网络应用程序中使用 twisted 异步运行任务?
  • If I leave the worker process declared in my Procfile but keep the scaling at web=1 worker=0, will my queued processes eventually be run on my available web dyno? Or will the queued processes never run?
  • If the queued processes will never run, is there another way to do this short of, for example, using twisted in my web app to run the tasks asynchronously?

附加信息

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()

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

The logic in the main app that enqueues a process looks like this:

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

q.enqueue(myfunction, myargument)

推荐答案

修改Procfile如下:

web: bin/web

现在创建bin目录,并创建文件bin/web,如下所示:

Now create the bin directory, and create the file bin/web to look like this:

#!/bin/bash
python app.py &
python worker.py

确保你给这个文件可执行权限:

Make sure you give this file the executable permission:

$ chmod +x bin/web

这篇关于仅使用 1 个 web dyno 和 0 个 worker dyno 运行 Heroku 后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆