使用SQLite作为代理的Celery + Flask,在调用任务时出错

时间:2015-12-10 08:27:05

标签: python celery

我正在尝试让Flask与Ceite一起使用SQLite作为后端。但是,使用以下代码:

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

启动工人后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

代码:

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

怎么了?我已经尝试使用Google搜索Sqllite / SQLAlchemy / Flask / Celery的任意组合,但一直无法找到解决方案。

2 个答案:

答案 0 :(得分:3)

正如您在跟踪中看到的,您的应用仍在尝试连接到rabbitmq-server。这意味着它没有正确配置。我没理解你使用CELERY_BROKER_URL代替BROKER_URL的原因。快速解决方案可能会改变这一点:

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

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

让我们看看它是如何运作的

答案 1 :(得分:0)

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

错误通常表示目标端口不可用或被某种防火墙阻止。

File "C:\Users\Robus\banking_virtenv\lib\site-packages\amqp\transport.py", line 95, in __init__
        raise socket.error(last_err)
    error: [Errno 10061] No connection could be made because the target machine actively refused it

看起来它正在尝试使用AMQP传输进行连接。请检查您的配置。