Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Celery in Django with just the DB?

Looking at https://docs.celeryq.dev/en/v5.2.7/getting-started/backends-and-brokers/index.html it sounds pretty much as if it's not possible / desirable. There is a section about SQLAlchemy, but Django does not use SQLAlchemy.

In way older docs, there is https://docs.celeryq.dev/en/3.1/getting-started/brokers/django.html .

Is it possible with recent Celery / Django versions to use Celery with just the database for storing messages / results?

like image 294
Martin Thoma Avatar asked Nov 15 '25 02:11

Martin Thoma


1 Answers

Yes you can totally do this, even if it's not the most performant/recommended way to do. I use it for simple projects in which I don't want to add Redis.

To do so, first, add SQLAlchemy v1 as a dependency in your project: SQLAlchemy = "1.*"

Then in your settings.py:

  • if you use PostgreSQL: CELERY_BROKER_URL = sqla+postgresql://user:[email protected]:5432/dbname
  • if you use SQLite: CELERY_BROKER_URL = "sqla+sqlite:///" + os.path.join(BASE_DIR, 'your_database.db') . Note that the folder holding the database must be writable. For example if your database is located in project/dbfolder/database.db, chmod 777 project/dbfolder will do the trick.

As a sidenote, I'm using django-celery-results to store results of my tasks. This way, I have a fully featured Celery without using other tech tool (like rabbitMQ or Redis) in my stack.

like image 186
David D. Avatar answered Nov 17 '25 15:11

David D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!