Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery: Use PostgreSQL instead of RabbitMQ

Is it possible to use a different message broker with celery?

For example: I would like to use PostgreSQL instead of RabbitMQ.

AFAIK it is only supported in the result backend: http://docs.celeryproject.org/en/latest/userguide/configuration.html#database-backend-settings

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/

like image 599
guettli Avatar asked Nov 24 '17 12:11

guettli


1 Answers

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

Queuing tasks

In [1]: from demo import add

In [2]: add.delay(1,2)
Out[2]: <AsyncResult: 4853190f-d355-48ae-8aba-6169d38fad39>

Worker results:

[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

Tested on latest(celery==4.1.0, kombu==4.1.0, SQLAlchemy==1.1.1) versions.

like image 171
Pandikunta Anand Reddy Avatar answered Oct 16 '22 16:10

Pandikunta Anand Reddy