我是celery的新手,我想使用它,但是我不知道我何时用delay()
调用任务?在添加新任务之后,我必须如何正确执行此任务?我使用了一个当前项目并对其进行了扩展,但是旧任务可以正常工作,而我的任务却不能。
目前的app1 / task.py:
from __future__ import absolute_import, unicode_literals
import logging
logger = logging.getLogger('notification')
@shared_task
def send_message_to_users(users, client_type=None, **kwargs):
.
.
#doing something here
.
.
logger.info(
'notification_log',
exc_info=False,
)
)
这是我在app2 / task.py中的代码:
@shared_task
def update_students_done_count(homework_id):
homework_students = HomeworkStudent.objects.filter(homework_id=homework_id)
students_done_count = 0
for homework_student in homework_students:
if homework_student.student_homework_status:
students_done_count += 1
homework = get_object_or_404(HomeWork, homework_id=homework_id)
homework.students_done_count = students_done_count
homework.save()
logger.info(
'update_students_done_count_log : with id {id}'.format(id=task_id),
exc_info=False,
)
两个任务如何调用的示例:
send_message_to_users.delay(users = SomeUserList)
update_students_done_count.delay(homework_id=SomeHomeWorkId)
project / celery.py:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hamclassy.settings')
app = Celery('hamclassy')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
projetc / init.py:
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']
对任务进行任何更改后,我都会通过以下命令运行工作程序:
celery -A project worker -l info
当我在端点中调用update_students_done_count.delay(homework_id=1)
时,日志向我显示recived task
,但是我等待任务的数量却没有执行。有什么主意吗?谢谢