Flask-Celery-Helper icon indicating copy to clipboard operation
Flask-Celery-Helper copied to clipboard

Connects to default amqp

Open batousik opened this issue 6 years ago • 3 comments

Following terminal output

`celery -A app.celery worker --loglevel=info

-------------- celery@ubuntu v4.1.0 (latentcall) ---- **** ----- --- * *** * -- Linux-4.13.0-16-generic-x86_64-with-Ubuntu-17.10-artful 2017-11-15 19:39:02 -- * - **** ---

  • ** ---------- [config]
  • ** ---------- .> app: main:0x7fda1bad4c90
  • ** ---------- .> transport: amqp://guest:**@localhost:5672//
  • ** ---------- .> results: disabled://
  • *** --- * --- .> concurrency: 4 (prefork) -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker) --- ***** ----- -------------- [queues] .> celery exchange=celery(direct) key=celery

[tasks] . app.celerytasks.mailboxsavertask.save_mailbox_items . app.celerytasks.mailboxsavertask.sumf

[2017-11-15 19:39:02,239: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused. Trying again in 2.00 seconds... `

batousik avatar Nov 15 '17 16:11 batousik

same problem in the application mode of Flask since the celery instance was setup like celery = Celery() the celery something.celery worker goes to default amqp at localhost which is not expected...

Fisherworks avatar Jan 03 '18 15:01 Fisherworks

an independent celery_worker works in this case can't figure out why though... http://liyangliang.me/posts/2015/11/using-celery-with-flask/

Fisherworks avatar Jan 03 '18 15:01 Fisherworks

Just need some modifications for your code. Take the Factory Example in README

# extensions.py
from flask_celery import Celery

celery = Celery()
# application.py
from flask import Flask
from extensions import celery

def create_app():
    app = Flask(__name__)
    app.config['CELERY_IMPORTS'] = ('tasks.add_together', )
    app.config['CELERY_BROKER_URL'] = 'redis://localhost'
    app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost'
    celery.init_app(app)

    # Make configured Celery instance attach to Flask
    app.celery = celery

    return app
# tasks.py
from extensions import celery

@celery.task()
def add_together(a, b):
    return a + b
# manage.py
from application import create_app

app = create_app()

# Use `celery -A manage.celery worker` to start celery worker
celery = app.celery

app.run()

This works for me.

fkztw avatar Jun 11 '18 13:06 fkztw