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

Rob*_*bus 10 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
Run Code Online (Sandbox Code Playgroud)

启动一个worker后,我在尝试调用一个虚拟任务时遇到这个错误:

error: [Errno 10061] No connection could be made because the target machine actively refused it
Run Code Online (Sandbox Code Playgroud)

码:

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

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

Rus*_*tem 6

正如您在跟踪中看到的,您的应用程序仍在尝试连接到 rabbitmq-server。这意味着它没有正确配置。我不明白你为什么使用CELERY_BROKER_URL而不是BROKER_URL. 快速解决方案可能是,改变这个:

celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
Run Code Online (Sandbox Code Playgroud)

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

让我们来看看它是如何工作的


mas*_*nun 0

error: [Errno 10061] No connection could be made because the target machine actively refused it  
Run Code Online (Sandbox Code Playgroud)

该错误通常意味着目标端口不可用或被某种防火墙阻止。

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
Run Code Online (Sandbox Code Playgroud)

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