0

Not sure why it throws this key error.

My project/celery.py:

import os
from celery import Celery



# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

app = Celery('project')


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

app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

my project/init.py:


from .celery import app as celery_app

__all__ = ('celery_app',)

My app/tasks.py

from celery import shared_task
from celery.schedules import crontab
from project.celery import app


@shared_task
def my_test():
   

    print('Celery from task says Hi')

    
app.conf.beat_schedule = {
    'my-task-every-2-minutes': {
        'task': 'my_test',
        'schedule': crontab(minute='*/1'),
    },
}


when I run the command: celery -A project beat -l info

I can see the trigger every 1 min there

  • [2022-12-22 12:38:00,005: INFO/MainProcess] Scheduler: Sending due task my-task-every-2-minutes (celery_app.my_test)

when running celery -A project worker -l info

and triggers sends info I get KeyError: my_test

It seems that it cannot find the task with that key

but from my info everything running fine :


 -------------- [email protected] v5.2.7 (dawn-chorus)
--- ***** ----- 
-- ******* ---- macOS-10.13.6-x86_64-i386-64bit 2022-12-22 12:43:22
- *** --- * --- 
- ** ---------- [config]
- ** ---------- .> app:         project:0x10c757610
- ** ---------- .> transport:   redis://localhost:6379//
- ** ---------- .> results:     redis://localhost:6379/
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

[tasks]
  . project.celery.debug_task
  . user_statistic_status.tasks.my_test

3
  • It seems like you need to use the whole task name in app.conf.beat_schedule. The task name is printed one you started a celery worker or beat instance. It is project.celery.debug_task. Commented Dec 22, 2022 at 14:08
  • when I run the task in the shell with my_test.delay() it works fine Commented Dec 22, 2022 at 14:42
  • cool it's working now when passing user_statistic_status.tasks.my_test Commented Dec 22, 2022 at 14:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.