Our great sponsors
-
django-pgpubsub
A distributed task processing framework for Django built on top of the Postgres NOTIFY/LISTEN protocol.
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
Rather than relying on signals and celery, we may want to consider borrowing a pattern from the land of Postgres: Triggers and NOTIFY/LISTEN. The library django-pgpubsub offers a django application layer abstraction of this pattern. We can refactor our above solution using django-pgpubsub to look as follows:
Note that have now ditched our post_save signal from the previous solution. As we pointed out, signals can easily be missed. Instead, we are using a Postgres trigger : defining the listener as it is above makes use of the django-pgtrigger library to write a Postgres trigger to our database, the job of which is to notify our channel whenever a Comment is inserted in to the database. Triggers are far more robust than signals for detecting database write events; application level triggers can easily be missed, whereas triggers will always be executed.
Related posts
- run scheduled job using django orm as broker
- django-pgpubsub: A lightweight alternative to Celery built on top of the Postgres NOTIFY/LISTEN protocol
- django-pgpubsub: Distributed Django Tasks using Postgres NOTIFY
- windows-friendly replacement for django=background-tasks?
- push notifications - reminders (Django Rest Framework + Postgresql)