我尝试过使用Celery Task& Django将框架序列化器放在同一个类中,具有多重继承。
from celery import Task
class ReceiveSerializer(Task, serializers.Serializer):
def run(self, source, *args, **kwargs):
self.save()
def save(self, **kwargs):
# call long running save method
我收到了错误,
File "<>\serializers.py", line 217, in <module>
class ReceiveSerializer(Task, serializers.Serializer):
File "<>\workspace\www\lib\site-packages\celery-3.1.20-py2.7.egg\celery\app\task.py", line 199, in __new_
_
tasks.register(new(cls, name, bases, attrs))
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
save方法必须在db中创建长对象列表(有时超过5分钟)。我不希望用户等待很长时间。 有没有办法做到这一点。
答案 0 :(得分:0)
可以使用Mixin吗?
class YourMixin:
# if you want to trigger the task on save:
def save(self, *args, **kwargs):
ret = super().save(*args, **kwargs)
some_task.apply_async((
self.__class__.__name__,
self.pk,
))
return ret
@task()
def some_task(model_name, model_id):
my_model = apps.get_model('django_app_name.{}'.format(model_name))
obj = my_model.objects.get(pk=model_id)