Celery不会将任务排队到远程服务器上的代理,而是将任务添加到localhost

时间:2017-03-17 14:40:30

标签: python django celery

celery.py

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

app = Celery('project', broker='amqp://foo:bar@remoteserver:5672', backend='amqp')
# app = Celery('project')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

tasks.py(在app文件夹中)

from __future__ import absolute_import, unicode_literals
from celery import shared_task    

@shared_task
def addnum(x, y):
    return (x + y)

当我调用此任务时:

addnum.delay(3, 5)

它返回:

<AsyncResult: 82cb362a-5439-4c1c-9c64-b158a9a48786>

但芹菜工人只是坐在那里等待任务,但没有收到任何:

[2017-03-17 13:48:36,869: INFO/MainProcess] celery@gauravrajput ready.

问题是任务没有排队到远程rabbitmq服务器。

当我将Celery初始化为:

app = Celery('project')

然后启动Celery工作,它开始接收并完成任务。

[2017-03-17 14:02:13,558: INFO/MainProcess] celery@gauravrajput ready.
[2017-03-17 14:02:13,560: INFO/MainProcess] Received task: app.tasks.addnum[82cb362a-5439-4c1c-9c64-b158a9a48786]  

1 个答案:

答案 0 :(得分:0)

我发现rabbitmq-server正在我的localhost上运行。 Idk为什么,但是即使在将远程RabbitMQ服务器明确声明为我的代理之后,任务也被排队到localhost而不是远程RabbitMQ服务器。但是,只需在我的localhost上停止rabbit-mq服务器就可以解决问题。

sudo -u rabbitmq rabbitmqctl stop