Skip to main content
 首页 » 编程设计

python之Django:Celery Worker 没有开始(没有任何错误)

2024年08月23日28over140

我正在尝试在 Google Compute Engine 上托管的 Ubuntu 14.04 机器上的 rabbitmq 服务器支持的 Django 应用程序中配置 djcelery 。

尝试使用以下命令以 Debug模式启动 celery:python manage.py celery worker -B -E --loglevel=debug,命令终止,输出如下:

[2016-03-24 12:16:09,568: DEBUG/MainProcess] | Worker: Preparing bootsteps. 
[2016-03-24 12:16:09,571: DEBUG/MainProcess] | Worker: Building graph... 
[2016-03-24 12:16:09,572: DEBUG/MainProcess] | Worker: New boot order: {Timer, Hub, Queues (intra), Pool, Autoscaler, StateDB, Autoreloader, Beat, Consumer} 
[2016-03-24 12:16:09,575: DEBUG/MainProcess] | Consumer: Preparing bootsteps. 
[2016-03-24 12:16:09,576: DEBUG/MainProcess] | Consumer: Building graph... 
[2016-03-24 12:16:09,577: DEBUG/MainProcess] | Consumer: New boot order: {Connection, Events, Mingle, Tasks, Control, Agent, Heart, Gossip, event loop} 
<user>@<gce.host>:~/path/to/my/project$ 

此问题的可能原因是什么? 在我的本地 ubuntu 机器上运行相同的设置,据我所知,我已经在我的云服务器上执行了所有步骤。


附加信息:我验证过的事情

  1. RabbitMQ 服务器运行良好。日志文件的输出:

=INFO REPORT==== 24-Mar-2016::17:02:14 === accepting AMQP connection <0.209.0> (127.0.0.1:42326 -> 127.0.0.1:5672)

=INFO REPORT==== 24-Mar-2016::17:02:14 === accepting AMQP connection <0.219.0> (127.0.0.1:42327 -> 127.0.0.1:5672)

=INFO REPORT==== 24-Mar-2016::17:02:17 === accepting AMQP connection <0.229.0> (127.0.0.1:42328 -> 127.0.0.1:5672)

  1. 端口 5672 在我的机器上打开。我还打开了端口:tcp:5555、tcp:4369、tcp:15672、tcp:5671 作为提到here (为了安全起见)。

Celery在我项目中的配置:

安装了 celery 和 django-celery 包。创建了 rabbitMQ 用户并使用命令设置其权限:

sudo rabbitmqctl add_user <user> <password> 
sudo rabbitmqctl set_permissions -p / <user> ".*" ".*" ".*" 

在settings.py文件中,我添加了:

import djcelery 
djcelery.setup_loader() 
 
MIDDLEWARE_CLASSES = [ 'django.middleware.transaction.TransactionMiddleware', 
                       ..] 
 
INSTALLED_APPS = ['djcelery', 
                  ..] 

celery.py 的内容如下:

from __future__ import absolute_import 
 
import os 
 
from datetime import timedelta 
from celery import Celery 
from celery.schedules import crontab 
 
from django.conf import settings 
 
 
# set the default Django settings module for the 'celery' program. 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<my_project>.settings') 
 
app = Celery('<my_project>') 
 
# Using a string here means the worker will not have to 
# pickle the object when using Windows. 
app.config_from_object('<my_project>.settings') 
# app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) 
 
app.conf.update( 
    CELERY_ACCEPT_CONTENT = ['json'], 
    CELERY_TASK_SERIALIZER = 'json', 
    CELERY_RESULT_SERIALIZER = 'json', 
    BROKER_URL = 'amqp://<user>:<password>@localhost:5672//', 
    # BROKER_URL = 'django://', 
    CELERY_RESULT_BACKEND = "amqp", 
    CELERY_IMPORTS = ("<module1>.tasks", "<module2>.tasks.tasks", "<module3>.tasks.tasks"), 
    CELERY_ALWAYS_EAGER = False, 
    # CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend', 
    # CELERY_TIMEZONE = 'Europe/London' 
    CELERY_TIMEZONE = 'UTC', 
    CELERYBEAT_SCHEDULE = { 
        'debug-test': { 
            'task': '<module1>.tasks.test_celery', 
            'schedule': timedelta(seconds=5), 
            # 'args': (1, 2) 
        }, 
    } 
) 

请您参考如下方法:

我终于解决了这个问题。我系统上的 celery 和 django-celery 包的版本不同。

ubuntu@my-host:~/path/to/project$ pip freeze | grep celery 
celery==3.1.21 
django-celery==3.1.17 

将 celery 版本更改为 3.1.17 修复了它。要更改 pip 的包版本,请使用:

ubuntu@my-host:~/path/to/project$ sudo pip install -I celery==3.1.17