Flask + Celery + Redis:使用者:无法连接到amqp:// guest:**@127.0.0.1:5672 //:超时

时间:2018-08-02 16:04:04

标签: python flask celery celerybeat

我设置了一个简单的芹菜任务。要运行它,我首先关闭了redis服务器,然后激活了虚拟环境并输入“ celery beat”,打开了一个进入虚拟环境的新终端窗口,并输入了“ celery worker”

Flask==1.0.2
celery==4.2.1
requests==2.19

这是随后的错误消息:

  

消费者:无法连接到amqp:// guest:**@127.0.0.1:5672 //:已定时   

这是执行“ celery beat”后显示的配置详细信息:

  

配置->       。经纪人-> amqp:// guest:** @ localhost:5672 //       。 loader-> celery.loaders.default.Loader       。调度程序-> celery.beat.PersistentScheduler       。 db-> celerybeat-schedule       。日志文件-> [stderr] @%警告       。 maxinterval-> 5.00分钟(300秒)

flask-proj / app / __ init __。py

from flask import Flask, request, jsonify
from celery import Celery
import celeryconfig

app = Flask(__name__)
app.config.from_object('config')

def make_celery(app):
    # create context tasks in celery
    celery = Celery(
        app.import_name,
        broker=app.config['BROKER_URL']
    )
    celery.conf.update(app.config)
    celery.config_from_object(celeryconfig)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)

    celery.Task = ContextTask

    return celery

celery = make_celery(app)

@app.route("/")
def hello():
    return "Hello World!"

flask-proj / tasks / test.py

import celery

@celery.task()
def print_hello():
    logger = print_hello.get_logger()
    logger.info("Hello")

flask-proj / config.py

import os

REDIS_HOST = "127.0.0.1" REDIS_PORT = 6379 BROKER_URL = environ.get('REDIS_URL', "redis://{host}:{port}/0".format(
    host=REDIS_HOST, port=str(REDIS_PORT))) CELERY_RESULT_BACKEND = BROKER_URL

flask-proj / celeryconfig.py

from celery.schedules import crontab

CELERY_IMPORTS = ('app.tasks.test')
CELERY_TASK_RESULT_EXPIRES = 30
CELERY_TIMEZONE = 'UTC'

CELERY_ACCEPT_CONTENT = ['json', 'msgpack', 'yaml']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

CELERYBEAT_SCHEDULE = {
    'test-celery': {
        'task': 'app.tasks.test.print_hello',
        # Every minute
        'schedule': crontab(minute="*"),
    }
}

如果需要提供其他详细信息,请告诉我。

3 个答案:

答案 0 :(得分:1)

amqp是rabbitmq,而不是redis。

Redis通常是

{key:lst[0] for key, lst in dct.items()}

我将手动配置以查看其是否有效。

redis://:password@hostname:port/db_number

答案 1 :(得分:0)

celery.conf.update(app.config) 函数中删除make_celery()行,因此就像

def make_celery(app):
    # create context tasks in celery
    celery = Celery(
        app.import_name,
        broker=app.config['BROKER_URL']
    )
    celery.conf.update(app.config) # remove this line.
    celery.config_from_object(celeryconfig)
    TaskBase = celery.Task

flask-proj/config.py的粘贴内容复制到flask-proj/celeryconfig.py

因此 flask-proj/celeryconfig.py 会变得像

from celery.schedules import crontab

import os

REDIS_HOST = "127.0.0.1"
REDIS_PORT = 6379
BROKER_URL = os.environ.get(
    'REDIS_URL', "redis://{host}:{port}/0".format(
        host=REDIS_HOST, port=str(REDIS_PORT)))
CELERY_RESULT_BACKEND = BROKER_URL

CELERY_IMPORTS = ('app.tasks.test')
CELERY_TASK_RESULT_EXPIRES = 30
CELERY_TIMEZONE = 'UTC'

CELERY_ACCEPT_CONTENT = ['json', 'msgpack', 'yaml']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

CELERYBEAT_SCHEDULE = {
    'test-celery': {
        'task': 'app.tasks.test.print_hello',
        # Every minute
        'schedule': crontab(minute="*"),
    }
}

答案 2 :(得分:0)

在Django中也有同样的问题,但事实证明我的问题是在settings.py中使用“ BROKER_URL”而不是“ CELERY_BROKER_URL”。 Celery找不到URL,并且默认使用Rabbitmq端口而不是redis端口。