简体   繁体   中英

Django-Celery No Periodic Outputs

I am trying to use Celery to create periodic tasks in my application. However, I cannot see the outputs of the periodic task that I wrote.

The backend is on a Windows-based redis-server. The server is up and running.

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)

However, when I ran celery -A project worker --pool=solo -l INFO , I get:

 -------------- 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. 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.

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

so in CLI, you need to execute:

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

Cdlt,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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