Should django model object instances be passed to celery?

后端 未结 2 1086
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 16:42
# models.py
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30         


        
相关标签:
2条回答
  • 2020-12-14 17:04

    I believe it is better and safer to pass PK rather than the whole model object. Since PK is just a number, serialization is also much simpler. Most importantly, you can use a safer sarializer (json/yaml instead of pickle) and have a peace of mind that you won't have any problems with serializing your model.

    As this article says:

    Since Celery is a distributed system, you can't know in which process, or even on what machine the task will run. So you shouldn't pass Django model objects as arguments to tasks, its almost always better to re-fetch the object from the database instead, as there are possible race conditions involved.

    0 讨论(0)
  • 2020-12-14 17:22

    Yes. If there are millions of records in the database then this probably isn't the best approach, but since you have to go through all many millions of the records, then pretty much no matter what you do, your DB is going to get hit pretty hard.

    Here are some alternatives, none of which I'd call "better", just different.

    1. Implement a pre_save signal handler for your Person class that does the .title() stuff. That way your first_name/last_names will always get stored correctly in the db and you'll not have to do this again.
    2. Use a management command that takes some kind of paging parameter...perhaps use the first letter of the last name to segment the Persons. So running ./manage.py my_task a would update all the records where the last name starts with "a". Obviously you'd have to run this several times to get through the whole database
    3. Maybe you can do it with some creative sql. I'm not even going to attempt here, but it might be worth investigating.

    Keep in mind that the .save() is going to be the harder "hit" to the database then actually selecting the millions of records.

    0 讨论(0)
提交回复
热议问题