Celery不将任务排队到远程服务器上的代理,而是将任务添加到本地主机 [英] Celery not queuing tasks to broker on remote server, adds tasks to localhost instead

查看:49
本文介绍了Celery不将任务排队到远程服务器上的代理,而是将任务添加到本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

celery.py

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(在应用程序文件夹中)

tasks.py (in the app folder)

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>

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

but celery worker just sits there waiting for tasks but doesn't receive any:

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

问题在于任务没有排队到远程Rabbitmq服务器.

The problem is that the tasks are not being queued to the remote rabbitmq server.

当我将Celery初始化为:

When I initialize Celery as:

app = Celery('project')

然后启动Celery worker,它开始接收并完成任务.

and then start Celery worker, it started to receive and complete tasks.

[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]  

推荐答案

我发现Rabbitmq服务器正在本地主机上运行.这就是为什么,即使在明确将远程RabbitMQ服务器声明为我的代理之后,任务却仍被排队到本地主机而不是远程RabbitMQ服务器.但是,只需在本地主机上停止Rabbit-mq服务器即可解决此问题.

I found out that rabbitmq-server was running on my localhost. Idk why but the tasks were being queued to the localhost instead of remote RabbitMQ server even after explicitly declaring the remote RabbitMQ server as my broker. However, simply stopping the rabbit-mq server on my localhost fixed the issue.

sudo -u rabbitmq rabbitmqctl stop

这篇关于Celery不将任务排队到远程服务器上的代理,而是将任务添加到本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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