问题描述
是否可以将其他消息代理与芹菜一起使用?
Is it possible to use a different message broker with celery?
例如:我想使用PostgreSQL而不是RabbitMQ.
For example: I would like to use PostgreSQL instead of RabbitMQ.
AFAIK仅在结果后端中受支持:http://docs.celeryproject.org/en/latest/userguide/configuration.html#database-backend-settings
AFAIK it is only supported in the result backend: http://docs.celeryproject.org/en/latest/userguide/configuration.html#database-backend-settings
自PostgreSQL 9.5起,已有 SKIP LOCKED
(跳过锁定),可实现强大的消息/工作队列.参见 https://blog.2ndquadrant.com/what-is-select-skip-locked-in-postgresql-9-5/
Since PostgreSQL 9.5 there is SKIP LOCKED
which enables implementing robust message/work queues. See https://blog.2ndquadrant.com/what-is-select-skip-locked-for-in-postgresql-9-5/
推荐答案
是的,您可以使用postgres作为代理,而不是Rabbitmq.这是一个简单的示例来演示它.
Yes, you can use postgres as broker instead of rabbitmq. Here is a simple example to demonstrate it.
from celery import Celery
broker = 'sqla+postgresql://user:pass@host/dbname'
app = Celery(broker=broker)
@app.task
def add(x, y):
return x + y
排队任务
In [1]: from demo import add
In [2]: add.delay(1,2)
Out[2]: <AsyncResult: 4853190f-d355-48ae-8aba-6169d38fad39>
工作人员结果:
[2017-12-02 08:11:08,483: INFO/MainProcess] Received task: t.add[809060c0-dc7e-4a38-9e4e-9fdb44dd6a31]
[2017-12-02 08:11:08,496: INFO/ForkPoolWorker-1] Task t.add[809060c0-dc7e-4a38-9e4e-9fdb44dd6a31] succeeded in 0.0015781960000822437s: 3
在最新版本(celery == 4.1.0,kombu == 4.1.0,SQLAlchemy == 1.1.1)上进行了测试.
Tested on latest(celery==4.1.0, kombu==4.1.0, SQLAlchemy==1.1.1) versions.
这篇关于Celery:使用PostgreSQL代替RabbitMQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!