芹菜任务没有通过redis分配
Celery Task not getting assigned through redis
使用 Celery/redis 我尝试创建一个任务,但是在使用以下代码检查芹菜工作信息时
celery -A intranet_project worker -l info
我无法在那里添加任务。
Settings.py
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
init.py
from __future__ import absolute_import
# 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.py
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'intranet_project.settings')
app = Celery('intranet_project')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
my_task.py
from celery.decorators import task
from celery import shared_task
@shared_task
def add(a,b):
d = a + b
return d
以下是服务器日志
[任务]
. intranet_project.celery.debug_task
[2020-02-26 19:38:59,051: INFO/MainProcess] 连接到redis://localhost:6379//
[2020-02-26 19:38:59,160: INFO/MainProcess] mingle: 寻找邻居
[2020-02-26 19:39:00,379: INFO/MainProcess] 打成一片: 一个人
我没有看到您执行任务的代码。
view.py(或任何您想触发任务的地方)
from .my_tasks import add
def action(request):
add.delay(2,2)
然后您应该会在 celery 日志中看到如下一行:
[2020-02-26 14:11:40,765: INFO/MainProcess] 收到的任务: intranet_project.tasks.add[xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx]
使用 Celery/redis 我尝试创建一个任务,但是在使用以下代码检查芹菜工作信息时
celery -A intranet_project worker -l info
我无法在那里添加任务。
Settings.py
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
init.py
from __future__ import absolute_import
# 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.py
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'intranet_project.settings')
app = Celery('intranet_project')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
my_task.py
from celery.decorators import task
from celery import shared_task
@shared_task
def add(a,b):
d = a + b
return d
以下是服务器日志
[任务] . intranet_project.celery.debug_task
[2020-02-26 19:38:59,051: INFO/MainProcess] 连接到redis://localhost:6379// [2020-02-26 19:38:59,160: INFO/MainProcess] mingle: 寻找邻居 [2020-02-26 19:39:00,379: INFO/MainProcess] 打成一片: 一个人
我没有看到您执行任务的代码。
view.py(或任何您想触发任务的地方)
from .my_tasks import add
def action(request):
add.delay(2,2)
然后您应该会在 celery 日志中看到如下一行:
[2020-02-26 14:11:40,765: INFO/MainProcess] 收到的任务: intranet_project.tasks.add[xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx]