【问题标题】:Run 2 separate celery daemon on a same server在同一台服务器上运行 2 个单独的 celery 守护进程
【发布时间】:2013-12-09 03:53:27
【问题描述】:

我有一台带有 2 个项目的服务器,每个项目都有不同的用途,但都需要使用 celery 运行调度程序任务。

我想在同一台服务器上创建单独的 celery daemon,我尝试创建不同的配置文件并使用 2 个不同的进程运行它。

app1 任务.py

from celery import task
@task()
def task_1(data):
    print("run task 1.")

app1配置文件:

# Name of nodes to start, here we have a single node
CELERYD_NODES="w1"
# Where to chdir at start.
CELERYD_CHDIR="/usr/app1/task_scheduler/"
# Extra arguments to celeryd
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# Name of the celery config module.
CELERY_CONFIG_MODULE="celeryconfig"
# %n will be replaced with the nodename.
CELERYD_LOG_FILE="/tmp/%n.log"
CELERYD_PID_FILE="/tmp/%n.pid"

app1,celeryconfig.py

BROKER_URL = 'amqp://guest:guest@localhost:5672//'
CELERY_IMPORTS=("tasks")
CELERY_RESULT_BACKEND = "amqp"
CELERY_ENABLE_UTC = False

app2 任务.py

from celery import task
@task()
def task_2(data):
    print("run task 2.")

app2 配置与 app1 配置相同,除了 CELERYD_CHDIRCELERYD_NODES。 app2 celeryconfig.py 与 app1 相同

我的问题是当我在 app1 上添加新任务时

from tasks import *
if __name__ == "__main__":
   ...
   task_1.apply_async(args=data, eta=t)

task_1 在 app1 节点上按预期运行,但此任务都添加到 app2 节点。因为节点 2 没有定义此函数,所以在节点 2 日志上引发了 not_found 函数。它仍然可以工作,但我如何设置让 task_1 只发送到 app1 celery 节点。我的意思是节点 2 不会收到 task_1 任务。

【问题讨论】:

  • 我遇到了类似的问题。我通过更改其中一名工作人员及其相应的 redis 守护程序的端口来解决它。看到这个:serverfault.com/questions/527311/…

标签: python scheduled-tasks celery daemon


【解决方案1】:

可以使用虚拟主机来分隔应用程序

要在使用 RabbitMQ 时创建虚拟主机,您应该看到: http://www.rabbitmq.com/access-control.html

虚拟主机是传输 URL 的路径部分:

amqp://user:pass@host:port/vhost

初始斜线将被忽略,因此如果您的虚拟主机是/foo,您需要 包括额外的斜线,如下所示:

amqp://guest:guest@localhosts//foo

一个使用两个启动两个工作人员的示例初始化脚本配置 单独的应用程序:

CELERYD_NODES="w1 w2"
CELERYD_OPTS="-A:w1 proj1 -A:w2 proj2 --concurrency:w1=8 --concurrency:w2=4"

有关celery multi 选项语法的更多信息,请参阅: http://docs.celeryproject.org/en/latest/reference/celery.bin.multi.html

然后您只需将应用程序proj1 配置为使用与proj2 不同的虚拟主机。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2012-12-05
    • 2013-06-06
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多