【问题标题】:Celery daemon - how to configure it to run multiple tasks from multiple Flask applications?Celery daemon - 如何配置它以运行来自多个 Flask 应用程序的多个任务?
【发布时间】:2020-04-15 09:30:53
【问题描述】:

我有一个烧瓶应用程序myapp_A,它使用 celery 来运行一些异步任务。我已经将 celery 配置为作为守护进程运行。这是服务脚本。

/etc/default/celery:

# Name of nodes to start
CELERYD_NODES="w1"

# Absolute or relative path to the 'celery' command:
CELERY_BIN="/var/www/myapp_A.com/public_html/venv/bin/celery"

# App instance to use
CELERY_APP="myapp_A.celery"

# Where to chdir at start.
CELERYD_CHDIR="/var/www/myapp_A.com/public_html/"

# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"

# %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"
CELERYD_LOG_LEVEL="INFO"

# Workers should run as an unprivileged user.
CELERYD_USER="myuser"
CELERYD_GROUP="www-data"

# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1

/etc/init.d/celeryd:

来自here的Celery的通用之一。


现在我有另一个 Flask 应用 myapp_B 也需要 celery 来运行任务。

  • 我应该如何配置?
  • 我应该以不同的名称创建另一个守护进程吗?
  • 我应该如何为多个 celery 进程配置消息代理 (RabbitMQ)?

【问题讨论】:

    标签: flask rabbitmq celery daemon


    【解决方案1】:

    您可以使用单个守护进程来处理这两个应用程序。 一种方法是为不同的应用程序使用不同的队列名称 这是我正在使用的配置

    celery worker -A init_celery --quiet --loglevel=$WORKER_LOG_LEVEL --concurrency=4 --queues=que1,que2
    

    然后在每个应用程序中指定队列名称。使用

    CELERY_DEFAULT_QUEUE = 'que1'
    

    【讨论】:

    • 谢谢。那么我应该为CELERY_BINCELERY_APPCELERYD_CHDIR 配置什么。我应该将这些配置移动到相应的应用程序中吗?如果是,我应该如何将这些变量传递给守护程序脚本?
    • 以下官方文档中有一个很好的例子。 docs.celeryproject.org/en/latest/userguide/daemonizing.html。如果您遇到麻烦,请在此处发布。
    • 嗨,我确实阅读了文档。我不明白的是,当您在两个venvs 中安装了芹菜时,您如何定义CELERY_BIN?当我有两个应用程序时我应该在哪里配置CELERY_APP,当两个应用程序使用芹菜时应该在CELERYD_CHDIR 中启动哪个目录?
    • 一个规范是使用 CELERYD_NODES="worker1 worker2 worker3" 启动多个节点,然后使用 CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1" 为这些工作人员指定选项您可以通过将节点名称附加到参数来配置特定于节点的设置:您可以找到所有选项使用celery worker --help
    • 如果我想清楚CELERY_BIN 路径可以使用--executable 选项指定worker -A 来指定CELERY_APP --workdir 来指定CELERYD_CHDIR
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    相关资源
    最近更新 更多