Stas Navarici
Stas Navarici

Reputation: 131

@shared_task decorator doesn't work

The problem: @shared_task decorator doesn't work, when I import data from custom files. I mean, when I start celery, all tasks under @shared_task doesn't appear in list of tasks.

For example, in this case @shared_task decorator doesn't work:

from __future__ import absolute_import, unicode_literals

from celery import shared_task

from .models import foo


@shared_task
def my_foo_backup(id):
    my_foo = foo(....)
    ...
    ...

This is example, when @shared_task works:

from __future__ import absolute_import, unicode_literals

from celery import shared_task


@shared_task
def my_foo_backup(id):
    my_foo = foo(....)
    ...
    ...

Why?!?

Upvotes: 2

Views: 2886

Answers (2)

Abk
Abk

Reputation: 154

In my case method with @shared_task was listed in celery tasks but it is not passed to broker properly(I used redis broker).
But when calling the @app.task (Note: app was imported from project_folder.celery.py ) decorator method inside tasks.py it was working fine.

Another solution I found was to import project_folder.celery inside tasks.py even though it is not used and then use the @shared_task decorator.

Upvotes: 0

Stas Navarici
Stas Navarici

Reputation: 131

The solution to that problem was to move import commands inside the function. In that way it works fine and according to the rules of PEP8.

from __future__ import absolute_import, unicode_literals

from celery import shared_task


@shared_task
def my_foo_backup(id):
    from .models import foo

    my_foo = foo(....)
    ...
    ...

Upvotes: 4

Related Questions