简体   繁体   English

Django-Celery 没有周期性输出

[英]Django-Celery No Periodic Outputs

I am trying to use Celery to create periodic tasks in my application.我正在尝试使用 Celery 在我的应用程序中创建定期任务。 However, I cannot see the outputs of the periodic task that I wrote.但是,我看不到我编写的周期性任务的输出。

The backend is on a Windows-based redis-server.后端位于基于 Windows 的 redis 服务器上。 The server is up and running.服务器已启动并正在运行。

project/celery.py项目/芹菜.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项目/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)

However, when I ran celery -A project worker --pool=solo -l INFO , I get:但是,当我运行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.

And the problem is that the program did not output the number from add(x, y) that I hoped it to be.问题是程序没有 output 我希望它是add(x, y)中的数字。 Can anyone please point out where my problem is?谁能指出我的问题在哪里?

Thanks in advance.提前致谢。

You need to start celery beat , because that him that will read the database and execute your task.您需要启动celery beat ,因为他将读取数据库并执行您的任务。

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

so in CLI, you need to execute:所以在 CLI 中,你需要执行:

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

You can daemonize beat celery -A project beat -S django --detach你可以守护beat celery -A project beat -S django --detach

Cdlt, cdlt,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM