Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery + Flask with SQLite as broker, error when calling task

Tags:

python

celery

I'm trying to get Flask to work with Celery with SQLite as backend. With the following code, however:

CELERY_BROKER_URL = 'sqla+sqlite:///' + os.path.join(basedir, 'celery.db')

def make_celery(app):
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    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

After starting a worker, I get this error when trying to call a dummy task:

error: [Errno 10061] No connection could be made because the target machine actively refused it

Code:

@app.route('/test')
def test():
    t = add_together.delay(100,200)
    return str(t.wait())

What's wrong? I've tried googling any combination of Sqllite/SQLAlchemy/Flask/Celery, but have been unable to find a solution.

like image 875
Robus Avatar asked Dec 10 '15 08:12

Robus


1 Answers

As you can see in your trace, your app still trying to connect to rabbitmq-server. That means it's not configured properly. I didn't get why you're using CELERY_BROKER_URL instead of BROKER_URL. Quick solution may be, change this:

celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])

to

celery = Celery(app.import_name,
                broker='sqla+sqlite:///' + os.path.join(basedir, 'celery.db'),
                backend='db+sqlite:///' + os.path.join(basedir, 'celery_results.db'))

Let's check how it works

like image 93
Rustem Avatar answered Oct 23 '22 12:10

Rustem