芹菜.py文件:
from __future__ import absolute_import
import os
from celery import Celery
from vortex import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
app = Celery("myapp")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
任务.py 文件:
from celery import shared_task
@shared_task(bind=True)
def debug_task1(xyz):
print('Running a task using celery', str(xyz))
return True
views.py(当服务器开始运行时,将调用 debug_task1())
def hello(request):
try:
t = debug_task1.delay()
print("task status:", t.status)
print("task id:", t.id)
print("task result:", t.result)
设置.py
CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_TIMEZONE = TIME_ZONE
CELERY_ACCEPT_CONTENT = ["json"]
CELERY_ENABLE_REMOTE_CONTROL = True
CELERY_RESULT_SERIALIZER = "json"
CELERY_RESULT_CACHE_MAX = -1
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_TASK_ANNOTATIONS = {
"celery.chord_unlock": {"soft_time_limit": 300, "default_retry_delay": 5}
}
CELERY_TASK_RESULT_EXPIRES = 600
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_SERIALIZER = "json"
CELERY_TASK_ACKS_LATE = True
CELERY_TASK_SEND_SENT_EVENT = True
CELERY_TASK_REJECT_ON_WORKER_LOST = True
CELERY_TASK_ALWAYS_EAGER = False
CELERY_WORKER_PREFETCH_MULTIPLIER = 1
CELERY_WORKER_POOL_RESTARTS = True
CELERY_WORKER_HIJACK_ROOT_LOGGER = False
CELERYD_TASK_TIME_LIMIT = 24 * 3600 # A task can run for max of 24hrs
CELERYD_SEND_EVENTS = True
CELERYD_MAX_TASKS_PER_CHILD = 20
CELERYD_WORKER_LOST_WAIT = 59
服务器结果:
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
task status: PENDING
task id: 79e35cf7-0a3d-4786-b746-2d3dd45a5c16
task result: None
工人结果:
-------------- celery@XYZ v5.0.5 (singularity) #XYZ is the system name
--- ***** -----
-- ******* ---- Windows-XXX 2021-07-02 13:10:54
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: myapp:0x18d6ff223e0
- ** ---------- .> transport: redis://127.0.0.1:6379//
- ** ---------- .> results: redis://127.0.0.1:6379/
- *** --- * --- .> concurrency: 8 (solo)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. tasks.task.debug_task1
列出任务后,工作人员不显示任何内容(例如“连接到 redis”)。此外,任务的状态始终为待处理
我究竟做错了什么?