Django-Celery 没有周期性输出

Django-Celery No Periodic Outputs

我正在尝试使用 Celery 在我的应用程序中创建周期性任务。但是,我看不到我写的周期性任务的输出。

后端在基于 Windows 的 redis 服务器上。服务器已启动 运行.

project/celery.py

import os

from celery import Celery
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nttracker.settings')

app = Celery

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

app.conf.update(
    result_expires=3600,
)

@app.task
def add(x, y):
    z = x + y
    print(z)

app.conf.beat_schedule = {
    'add-every-30-seconds': {
        'task': 'tasks.add',
        'schedule': 30.0,
        'args': (16, 16)
    },
}

if __name__ == '__main__':
    app.start()

project/tests.py

from __future__ import absolute_import

import django
django.setup()
from .celery import app

@app.task
def add(x, y):
    return x + y

@app.task
def mul(x, y):
    return x * y

@app.task
def xsum(numbers):
    return sum(numbers)

但是,当我 运行 celery -A project worker --pool=solo -l INFO 时,我得到:

 -------------- celery@LAPTOP-A0OM125L v5.0.5 (singularity)  
--- ***** -----
-- ******* ---- Windows-10-10.0.19041-SP0 2021-06-04 16:45:29
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         nttracker:0x22698b76340      
- ** ---------- .> transport:   redis://127.0.0.1:6379//     
- ** ---------- .> results:
- *** --- * --- .> concurrency: 12 (solo)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . nttracker.celery.add
  . nttracker.celery.debug_task
  . nttracker.tasks.add
  . nttracker.tasks.mul
  . nttracker.tasks.xsum

[2021-06-04 16:45:30,017: INFO/MainProcess] Connected to redis://127.0.0.1:6379//
[2021-06-04 16:45:30,028: INFO/MainProcess] mingle: searching for neighbors
[2021-06-04 16:45:31,059: INFO/MainProcess] mingle: all alone
[2021-06-04 16:45:31,079: WARNING/MainProcess] c:\users\warren\onedrive\desktop\github_new\nttracker\venv\lib\site-packages\celery\fixups\django.py:203: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  warnings.warn('''Using settings.DEBUG leads to a memory

[2021-06-04 16:45:31,080: INFO/MainProcess] celery@LAPTOP-A0OM125L ready.

问题是程序没有从 add(x, y) 输出我希望的数字。谁能指出我的问题出在哪里?

提前致谢。

您需要启动 celery beat,因为他将读取数据库并执行您的任务。

安装:https://github.com/celery/django-celery-beat

所以在CLI中,你需要执行:

celery -A project worker --pool=solo -l INFO
celery -A project beat -S django

你可以守护节拍 celery -A project beat -S django --detach

Cdlt,