user3595632
user3595632

Reputation: 5730

Django celery: class-based task doesn't work

tasks.py

from celery import Task


class SimpleTask(Task):

    def run(self):
        print("run")

Execute python manage.py shell

In [3]: from products.tasks import SimpleTask

In [4]: task = SimpleTask()

In [6]: task.run()
run

Successfully work and no error logs come out in worker server.

Howerver,

In [7]: task.delay()
Out[7]: <AsyncResult: a2e90b17-2af9-49b4-82df-562955beaf69>

And worker server log shows errors:

[2016-11-05 18:44:03,171: ERROR/MainProcess] Received unregistered task of type None.
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:
b'[[], {}, {"callbacks": null, "chord": null, "errbacks": null, "chain": null}]' (77b)
Traceback (most recent call last):
  File "/Users/Chois/.pyenv/versions/3.5.1/envs/spacegraphy/lib/python3.5/site-packages/celery/worker/consumer/consumer.py", line 549, in on_task_received
    strategy = strategies[type_]
KeyError

I don't see why this happens. If I created function-based task using @shared_task it successfully works. But only class-based Task doesn't work.

Need helps, Thanks.

Upvotes: 3

Views: 874

Answers (1)

shapiy
shapiy

Reputation: 1155

I had the same issue with class based tasks, but not in Django. Fixed it by setting name attribute on the task.

class AddTask(celery.Task):
     name = 'AddTask'

Upvotes: 1

Related Questions