0

When I try to run this code it throws an error, I want to run task_number_one and print_test as a periodic task but it throws the same error for both.

from celery.task import periodic_task
from celery.schedules import crontab
from celery import task
@task()
def task_number_one():
    print("Celery Task Working...")

@periodic_task(run_every=crontab(minute='10', hour='6,8,9,10,11,12'))
def print_test():
   print(1)

task_number_one.delay()

I get this below error

[2019-09-18 17:21:17,093: ERROR/MainProcess] Received unregistered task of type 'page.tasks.task_number_one'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (77b)
Traceback (most recent call last):
  File "/home/naanal/venv/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 559, in on_task_received
    strategy = strategies[type_]
KeyError: 'page.tasks.task_number_one'
mageshwaran
  • 39
  • 1
  • 2
  • 10
  • You have to import this task into the file where you have your `Celery()` object stored I believe.. – Nf4r Sep 18 '19 at 18:29
  • Possible duplicate of [Celery Received unregistered task of type (run example)](https://stackoverflow.com/questions/9769496/celery-received-unregistered-task-of-type-run-example) – Ahmed Magdy Sep 18 '19 at 19:32

1 Answers1

0

If you are using celery v4, you register a task differently than you do with v3. For v4 use the following:

from myapp.celery import app

@app.task
def task_number_one():
    print("Celery Task Working...")
2ps
  • 15,099
  • 2
  • 27
  • 47