Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django's ORM in a Celery Task

How can a Celery task have access to the Django database-abstraction API? Does this need to be coded from scratch using one of the strategies for stand-alone Django ORM usage, or is there a more streamlined, built-in way or common practice?

It seems that nobody is asking this question. However, to me, it's fundamental.

This example implies that it's no big deal, but can someone explain how the session management and ORM scoping works between Celery and Django?

like image 723
Josh Pearce Avatar asked Jul 18 '11 14:07

Josh Pearce


1 Answers

By default Celery pickles its task parameters. Django model instances can be pickled too.

The catch is that pickling a model instance is like taking a snapshot of it at that time. Unpickling doesn't touch the database.

Whether this is good or bad depends, I suppose, on your needs. I tend to send a primary key into my tasks and re-query for the object in question.

like image 119
Dave Peck Avatar answered Oct 26 '22 19:10

Dave Peck