Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
Store result to database from celery worker vs return result

I'm using Celery w/ Django to process a certain task which returns a JSON value that needs to be put in a Model record. Right now I see 2 options to persist it in the Django database:

  1. Passing in the id of the record as part of the task signature. Then Celery can use it to update the record.
  2. Alternatively, I can return the result from the task and enable the django-db result backend for Celery, which will live in Celery's own task_result table. This means I'll have to persist the AsyncResult Id inside the record, and whenever the client requests the record I'll look up and see if the process is done or not.

To me it seems that option 1 is better, but since I haven't worked with Celery in recent years I want to know if there are downsides to it, and/or which situation would option 2 be better suited for.

Thanks!

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

No there is nothing wrong with the first approach.

tasks.py from app.models import your_model from celery import task

@task
def update_model(id):
    model_obj = your_model.objects.get(id=id)
    #do your stuffs here...

views.py

from app.tasks import update_model

def your_view(request):
    #your code
    update_model.delay(id_of_the_instance_you_want_to_update)

You can use this example code for atomic commits in the database. If you are worried about.(taken from celery docs)

from functools import partial
from django.db import transaction

from .models import Article, Log
from .tasks import send_article_created_notification

def create_article(request):
    with transaction.atomic():
        article = Article.objects.create(**request.POST)
        # send this task only if the rest of the transaction succeeds.
        transaction.on_commit(partial(
            send_article_created_notification.delay, article_id=article.pk))
        Log.objects.create(type=Log.ARTICLE_CREATED, object_pk=article.pk)
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda