# 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)
text_blob = models.CharField(max_length=50000)
# tasks.py
import celery
@celery.task
def my_task(person):
# example operation: does something to person
# needs only a few of the attributes of person
# and not the entire bulky record
person.first_name = person.first_name.title()
person.last_name = person.last_name.title()
person.save()
在我的应用程序中,我有类似的东西:
from models import Person
from tasks import my_task
import celery
g = celery.group([my_task.s(p) for p in Person.objects.all()])
g.apply_async()
- 芹菜泡菜 p 发给工人吧?
- 如果工作人员在多台机器上运行,整个 person 对象(以及主要不需要的庞大的 text_blob)是否会通过网络传输?有没有办法避免它?
如何有效且均匀地将 Person 记录分配给在多台机器上运行的工作人员?
这可能是一个更好的主意吗?如果 Person 有几百万条记录,它不会压倒数据库吗?
# tasks.py import celery from models import Person @celery.task def my_task(person_pk): # example operation that does not need text_blob person = Person.objects.get(pk=person_pk) person.first_name = person.first_name.title() person.last_name = person.last_name.title() person.save() #In my application somewhere from models import Person from tasks import my_task import celery g = celery.group([my_task.s(p.pk) for p in Person.objects.all()]) g.apply_async()