Search code examples
djangocelerycelery-taskcookiecutter-django

@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?!?


Solution

  • 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(....)
        ...
        ...