在Ubuntu中将CELERY作为守护进程运行

时间:2014-03-20 07:07:07

标签: ubuntu celery daemon

如何在ubuntu中将celery作为Daemon服务运行,而不是每次都运行“celery -A projname worker -l info”命令。

我正在使用芹菜3.1.8版....

3 个答案:

答案 0 :(得分:4)

您可以使用芹菜回购中的init.d script将芹菜变成守护进程(将其保存到/etc/init.d/)。您还需要为/etc/default/celeryd中的脚本创建configuration file

这是完整的celery doc

答案 1 :(得分:2)

您可以使用名为supervisord的工具将芹菜工作者作为守护程序。

这是一个简单的流程经理, 您可以找到有关如何使用supevisor配置文件配置工作人员的示例here

答案 2 :(得分:0)

这是很久以前问过的一个问题,但我认为值得给出答案。

您可以按照以下步骤操作:

  1. 创建一个芹菜用户
from fastapi import FastAPI, Response

app = FastAPI()

@app.post("/vector_image/")
async def image_endpoint():
    # img = ... # Create the image here
    return Response(content=img, media_type="image/png")
  1. 创建初始化脚本
sudo adduser celery

并将celeryd和过去的内容复制到创建的sudo nano celeryd 中。 然后

celeryd
  1. 创建配置文件
  • 为初始化脚本创建一个空的/ etc / default / celeryd配置文件
chmod +x celeryd
sudo mv celeryd /etc/init.d/
  • 根据您的项目编辑上述芹菜。
sudo touch /etc/default/celeryd
sudo vim /etc/default/celeryd
  1. 创建日志目录
 # Names of nodes to start
 #   most people will only start one node:
 CELERYD_NODES="worker1"
 #   but you can also start multiple and configure settings
 #   for each in CELERYD_OPTS
 #CELERYD_NODES="worker1 worker2 worker3"
 #   alternatively, you can specify the number of nodes to start:
 #CELERYD_NODES=10

 # Absolute or relative path to the 'celery' command:
 #CELERY_BIN="/usr/local/bin/celery"
 #CELERY_BIN="/virtualenvs/def/bin/celery"
 CELERY_BIN="/opt/django_projects/your_proj/your_env/bin/celery"

 # App instance to use
 # comment out this line if you don't use an app
 CELERY_APP="your_proj"
 # or fully qualified:
 #CELERY_APP="proj.tasks:app"

 # Where to chdir at start.
 CELERYD_CHDIR="/opt/django_projects/your_proj/"

 # Extra command-line arguments to the worker
 CELERYD_OPTS="--time-limit=300 --concurrency=8"
 # Configure node-specific settings by appending node name to arguments:
 #CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1"

 # Set logging level to DEBUG
 #CELERYD_LOG_LEVEL="DEBUG"

 # %n will be replaced with the first part of the nodename.
 CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
 CELERYD_PID_FILE="/var/run/celery/%n.pid"

 # Workers should run as an unprivileged user.
 #   You need to create this user manually (or you can choose
 #   a user/group combination that already exists (e.g., nobody).
 CELERYD_USER="celery"
 CELERYD_GROUP="celery"

 # If enabled pid and log directories will be created if missing,
 # and owned by the userid/group configured.
 CELERY_CREATE_DIRS=1
  1. 详细介绍初始化脚本
sudo mkdir /var/log/celery
sudo chown -R celery: /var/log/celery
  1. 启用守护程序
sudo sh -x /etc/init.d/celeryd start

好吧,希望对您有所帮助。 谢谢。