我正试图让Flask与Celery一起使用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
在启动一个工作者后,我在尝试调用一个伪任务时遇到了这个错误:
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())
怎么了?我试过在谷歌上搜索Sqllite/SQLAlchemy/Flask/Celery的任何组合,但一直找不到解决方案。
正如您在跟踪中看到的,您的应用程序仍在尝试连接到rabbitmq-服务器。这意味着它没有正确配置。我不明白你为什么用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'))
让我们检查一下它是如何工作的
error: [Errno 10061] No connection could be made because the target machine actively refused it
该错误通常意味着目标端口不可用或被某种防火墙阻止。
File "C:UsersRobusbanking_virtenvlibsite-packagesamqptransport.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传输进行连接。请检查您的配置。