我已经按照教程设置了celery/redis的相关设置。 celery 任务可以成功运行,但是 django admin 中的“任务结果”是空的,有 0 条记录。
这是我的设置.py:
CELERY_BROKER_URL = "redis://127.0.0.1:6379/0"
result_backend = 'redis://127.0.0.1:6379/0'
CELERY_RESULT_BACKEND = "django-db"
# CELERY_CACHE_BACKEND = 'django-cache'
CELERY_ACCEPT_CONTENT = ['application/json', ]
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# celery setting.
CELERY_CACHE_BACKEND = 'default'
# # django setting.
# CACHES = {
# 'default': {
# 'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
# 'LOCATION': 'my_cache_table',
# }
# }
CELERY_TASK_TIME_LIMIT = 30*60
芹菜.py:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
app = Celery('mysite',backend='redis://localhost:6379/1', broker='redis://localhost:6379/0')
app.config_from_object("django.conf:settings")
app.autodiscover_tasks()
app.loader.override_backends['django-db'] = 'django_celery_results.backends.database:DatabaseBackend'
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
[2023-05-14 19:08:08,457: INFO/MainProcess] Received task: OnlinePricing.tasks.add[9c082207-ea07-4962-8bee-da3bce084564]
[2023-05-14 19:08:11,467: INFO/MainProcess] Task OnlinePricing.tasks.add[9c082207-ea07-4962-8bee-da3bce084564] succeeded in 3.0159999999996217s: 8
我猜上面的任务结果从未存储在数据库中。我已经迁移了 django_celery_results。这个空记录问题的根本原因是什么?