我试图创建一个后台任务,Django,芹菜和Redis作为经纪人。当模型发布保存时,将从模型发送此任务。但问题是,相同的任务正在执行1000次(其他调试或添加任务运行良好)。我已经尝试过这个方法,但这并没有解决问题。请找到以下代码供您参考和帮助解决。提前谢谢。
models.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from student.tasks import create_student_subject_tryout_result, add
@receiver(post_save, sender=TryoutSubmission,
dispatch_uid='create_student_subject_tryout_result')
def result_calculation(sender, instance, **kwargs):
if instance.status == 'C':
print('Calculating result')
create_student_subject_tryout_result.delay(instance.student.id,
instance.tryout.id)celery.py
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'eec.settings')
app = Celery('eec')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.broker_transport_options = {'visibility_timeout': 3600} .
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')tasks.py
from celery import shared_task
import tryout.models
@shared_task(bind=True)
def create_student_subject_tryout_result(self, student_id, tryout_id):
tryout_submission=tryout.models.TryoutSubmission.objects.get(
student_id=student_id, tryout_id=tryout_id
)
tryout_questions = tryout_submission.tryout.tryoutquestion_set.all().count()
answered_qs = tryout_submission.tryout.tryoutanswersubmission_set.filter(
is_answered=True).count()
correct_ans = tryout_submission.tryout.tryoutanswersubmission_set.filter(
is_correct=True).count()
tryout_submission.total_questions = tryout_questions
tryout_submission.answered_questions = answered_qs
tryout_submission.correct_answers = correct_ans
tryout_submission.total_time = tryout_submission.end_time - tryout_submission.start_time
tryout_submission.save()
return "Result created"settings.py
CELERY_RESULT_BACKEND = 'django-db'
CELERY_CACHE_BACKEND = 'django-cache'
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'发布于 2022-10-04 06:45:25
如果你看你的接收器,你是在听你的模型上的post_save。
post_save信号还提供了一个boolean来表示是否创建了它。与其检查instance.status == 'C',不如检查创建的:
from django.db.models.signals import post_save
from django.dispatch import receiver
from student.tasks import create_student_subject_tryout_result, add
@receiver(post_save, sender=TryoutSubmission,
dispatch_uid='create_student_subject_tryout_result')
def result_calculation(sender, instance, raw, created, **kwargs):
if created:
print('Calculating result')
create_student_subject_tryout_result.delay(
instance.student.id,
instance.tryout.id
)https://stackoverflow.com/questions/73127161
复制相似问题